Skip to content

Instantly share code, notes, and snippets.

@himitsu-fushigi
Created July 10, 2021 14:48
Show Gist options
  • Save himitsu-fushigi/51a68f1348f2e4e450b9ab2c5d694693 to your computer and use it in GitHub Desktop.
Save himitsu-fushigi/51a68f1348f2e4e450b9ab2c5d694693 to your computer and use it in GitHub Desktop.
// @dev cargo doc --open will build a documentation of dependencies and their methods
// @dev alike C,C++ input and output is from standard io library
// @dev std has many features but this time we imported for input feature
use std::io;
// @dev rand is a package/dependency added later through toml
use rand::Rng;
// @dev main is the entry point in the rust program
fn main(){
// @dev println!() is a function to print output
println!("Guess the number");
// @dev this is immutable
let secret_number = rand::thread_rng().gen_range(1..101);
println!("The secret number is: {}", secret_number);
println!("Please input your guess!");
// @dev let is used to create the variables
// @dev mut keyword refers to mutable since the variables with let are immutable by deafult
// @dev String::new() is new instance of String - Kind of new object
// @dev finally created a mutable variable that is currently bound to a new, empty instance of a String
let mut guess = String::new();
// @dev calling stdin function from io module
io::stdin()
// @dev & is used to reference the variable and mut is used to remind it as a mutable else it'd be &guess
.read_line(&mut guess)
// @dev next line is handling the error if something arises while storing text
.expect("failed to read line!");
// @dev formatted output possible we could use named parameter as well.
println!("You guessed {}", guess);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment