Remove leading and trailing whitespace from a Rust string using the .trim() method.
Use the .trim() method on your String to remove leading and trailing whitespace before parsing or processing.
let clean = guess.trim();
In the guessing game example, this is chained directly to the input to convert it to a number:
let guess: u32 = guess.trim().parse().expect("Please type a number!");
The .trim() method removes any empty spaces from the start and end of a text string. You use this when reading user input because people often accidentally hit the spacebar or enter a newline character. It's like erasing the margins of a note before reading the main message.