From 3bb544989b889ba742eeb115ca77ddbc07379078 Mon Sep 17 00:00:00 2001 From: Camille Frantz Date: Mon, 23 Dec 2024 08:50:45 -0600 Subject: [PATCH] read_line insisting on keeping newline --- src/main.rs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/main.rs b/src/main.rs index a42041d..0add9b8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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(); } }