This question already has an answer here:
In the following code, I was expecting the message “wow” to be printed when the user enters “q”, but it does not.
fn main() {
let mut input = String::new();
io::stdin().read_line(&mut input)
.expect("failed to read line");
if input == "q" {
println!("wow") ;
}
}
Why is the message not printed as expected?
Your input string contains a trailing newline. Use
trim
to remove it:You could see this yourself by printing the value of the input