Start learning free

LESSON 69 OF 679 · STRINGS I

String Strip / Trim in Rust

Lesson

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.

STEP 1 OF 1
    Ctrl + Enter to check