std-method

str::trim

Borrow text without leading or trailing Unicode whitespace. Internal spaces stay in place; the original buffer is not mutated. Expected stdout (not executed): "hello world" Common mistake: Expecting trim to remove all spaces.

Formal Syntax & Structure

rust syntaxrust
fn trim(&self) -> &str

Working Example

Example Snippetrust
fn main() {    let text = "  hello world   ";    println!("{:?}", text.trim());}
Rust Reference