loop and break
This commit is contained in:
@ -17,26 +17,33 @@ fn main() {
|
||||
let mut guess = String::new();
|
||||
// could still call this without importing it via std::io::stdin
|
||||
// this can be one line, it is broken up for readability.
|
||||
io::stdin()
|
||||
// & indicates that this argument is a reference,
|
||||
// references are also immutable by default so &mut is needed
|
||||
.read_line(&mut guess)
|
||||
// expect handles the Err variant by crashing the program. There are other ways to handle
|
||||
// errors.
|
||||
.expect("Failed to read line");
|
||||
// read_line returns a `result` value. result is an enum. Each possible state is a variant.
|
||||
// Result's variants are Ok and Err
|
||||
loop {
|
||||
io::stdin()
|
||||
// & indicates that this argument is a reference,
|
||||
// references are also immutable by default so &mut is needed
|
||||
.read_line(&mut guess)
|
||||
// expect handles the Err variant by crashing the program. There are other ways to handle
|
||||
// errors.
|
||||
.expect("Failed to read line");
|
||||
// read_line returns a `result` value. result is an enum. Each possible state is a variant.
|
||||
// Result's variants are Ok and Err
|
||||
|
||||
// shadowing V
|
||||
let guess: u32 = guess.trim().parse().expect("please type a number for the love of god");
|
||||
// trim removes space and \n
|
||||
println!("you guessed: {guess}");
|
||||
// a match expression is made of arms. an arm is a pattern to match and code to run against it.
|
||||
// switch/case
|
||||
match guess.cmp(&secret_number) {
|
||||
Ordering::Less => println!("too small"),
|
||||
Ordering::Greater => println!("too big"),
|
||||
Ordering::Equal => println!("you win"),
|
||||
// shadowing V
|
||||
let guess: u32 = guess.trim().parse().expect("please type a number for the love of god");
|
||||
// trim removes space and \n
|
||||
// single colon is type annotation
|
||||
//
|
||||
println!("you guessed: {guess}");
|
||||
// a match expression is made of arms. an arm is a pattern to match and code to run against it.
|
||||
// switch/case
|
||||
match guess.cmp(&secret_number) {
|
||||
Ordering::Less => println!("too small"),
|
||||
Ordering::Greater => println!("too big"),
|
||||
Ordering::Equal => {
|
||||
println!("you win");
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// cargo doc --open
|
||||
|
Reference in New Issue
Block a user