Library reference

Remove spaces at both ends

Remove the spaces before and after a name. A space between two words stays in place. These examples use ordinary spaces.

str::trim

Simple example

Trim the single letter.

Start' a '
Resulta
main.rs
fn main() {
    println!("{}", " a ".trim());
}
Output
a
Advertisements

Everyday examples

Remove the extra spaces copied around a ticket number.

Start' B12 '
ResultB12
main.rs
fn main() {
    println!("{}", "  B12 ".trim());
}
Output
B12

Clean up the outside spaces on a shelf label.

Start' Red Apples '
ResultRed Apples
main.rs
fn main() {
    println!("{}", " Red Apples  ".trim());
}
Output
Red Apples

Clean up a name copied from a signup sheet.

Start' Ana '
ResultAna
main.rs
fn main() {
    println!("{}", "  Ana ".trim());
}
Output
Ana
Run the example
Terminal
rustc main.rs
./main
Official documentation