In Rust, the built-in .trim() method removes leading and trailing whitespace from a string:
// trim() removes leading and trailing spaces
let s = " hello ";
let result = s.trim();
// result == "hello"
Here is an example showing how .trim() works:
let s = " hello ";
let result = s.trim();
println!("{}", result); // "hello"
Assignment
Follow the Coding Tutorial and use .trim() to strip whitespace from s!
Hint
Use s.trim() and assign the result to result.
Free plan: 30 tutor messages. Premium is unlimited.
Saved
Ctrl + Enter to check