Start learning free

LESSON 68 OF 348 · STRINGS I

String Strip / Trim in Rust 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

    String Strip in Rust

    Keep going with RustAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free