Instead of trim_right_matches I would recommend using trim_right or better, just trim :
use std::io; fn main() { let mut correct_name = String::new(); io::stdin().read_line(&mut correct_name).expect("Failed to read line"); let correct_name = correct_name.trim(); if correct_name == "y" { println!("matched y!"); } else if correct_name.trim() == "n" { println!("matched n!"); } }
This last case handles many types of spaces:
Returns a fragment of a string with leading and trailing spaces removed.
Whitespace is defined according to the conditions of the Unicode White_Space derived kernel property.
Therefore, Windows / Linux / macOS should not matter.
You can also use the truncated length of the result to crop the original String , but in this case you should only use trim_right !
let trimmed_len = correct_name.trim_right().len(); correct_name.truncate(trimmed_len);
source share