How to Trim Whitespace from a String in Rust

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!");