read_line insisting on keeping newline

This commit is contained in:
Camille Frantz 2024-12-23 08:50:45 -06:00
parent fbe19464eb
commit 3bb544989b
Signed by: fyrfli
SSH Key Fingerprint: SHA256:0rb3/fMYNySVgUgOqOdmFnb+qhrk1C0Rc/Ak3AoyJug

View File

@ -1,17 +1,19 @@
use std::io;
use std::io::{self, Write};
use rand::Rng;
use std::cmp::Ordering;
// use std::str;
fn main() {
println!("Let's play the guessing game!");
let secret_number = rand::thread_rng().gen_range(1..=100);
loop {
println!(" What's your guess?");
print!("What's your guess? ");
io::stdout().flush().unwrap();
let mut guess = String::new();
io::stdin()
.read_line(&mut guess)
.expect("Sorry; something didn't quite work.");
let guess: u32 = match guess.trim().parse() {
let guess: u32 = match guess.trim_end().parse() {
Ok(num) => num,
Err(_) => continue,
};
@ -23,5 +25,6 @@ fn main() {
break;
}
}
// io::stdout().flush().unwrap();
}
}