loop and break
This commit is contained in:
@ -17,6 +17,7 @@ fn main() {
|
|||||||
let mut guess = String::new();
|
let mut guess = String::new();
|
||||||
// could still call this without importing it via std::io::stdin
|
// could still call this without importing it via std::io::stdin
|
||||||
// this can be one line, it is broken up for readability.
|
// this can be one line, it is broken up for readability.
|
||||||
|
loop {
|
||||||
io::stdin()
|
io::stdin()
|
||||||
// & indicates that this argument is a reference,
|
// & indicates that this argument is a reference,
|
||||||
// references are also immutable by default so &mut is needed
|
// references are also immutable by default so &mut is needed
|
||||||
@ -30,13 +31,19 @@ fn main() {
|
|||||||
// shadowing V
|
// shadowing V
|
||||||
let guess: u32 = guess.trim().parse().expect("please type a number for the love of god");
|
let guess: u32 = guess.trim().parse().expect("please type a number for the love of god");
|
||||||
// trim removes space and \n
|
// trim removes space and \n
|
||||||
|
// single colon is type annotation
|
||||||
|
//
|
||||||
println!("you guessed: {guess}");
|
println!("you guessed: {guess}");
|
||||||
// a match expression is made of arms. an arm is a pattern to match and code to run against it.
|
// a match expression is made of arms. an arm is a pattern to match and code to run against it.
|
||||||
// switch/case
|
// switch/case
|
||||||
match guess.cmp(&secret_number) {
|
match guess.cmp(&secret_number) {
|
||||||
Ordering::Less => println!("too small"),
|
Ordering::Less => println!("too small"),
|
||||||
Ordering::Greater => println!("too big"),
|
Ordering::Greater => println!("too big"),
|
||||||
Ordering::Equal => println!("you win"),
|
Ordering::Equal => {
|
||||||
|
println!("you win");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// cargo doc --open
|
// cargo doc --open
|
||||||
|
Reference in New Issue
Block a user