Skip to content

str::trim_left

str methods · Strings

Level: reference · for working programmers

Deprecated since Rust 1.33 — use str::trim_start instead. The page is here because the name is still in code written before it moved, and it still compiles; a new call site should not use it.

One line: The pre-1.33 name for trim_start.

pub fn trim_left(&self) -> &str

Stable since 1.0.0.

Renamed because "left" describes a position on screen and the method describes a position in the bytes. For right-to-left scripts the two are opposite, so the old name was actively misleading; start and end are unambiguous in every script.

Identical behaviour — delete left, write start.

Example

str_trim_left.rs in full — pasted here by tools/run_examples.py from the file CI compiles and runs.

#![allow(deprecated)]

fn main() {
    let s = "  padded  ";
    println!("{:?}", s.trim_left());
    println!("{:?}", s.trim_start());
    println!("identical: {}", s.trim_left() == s.trim_start());
}

Verified output of str_trim_left.rs — regenerated by tools/run_examples.py, never hand-typed.

"padded  "
"padded  "
identical: true

See also

str::trim_left in the standard library ↗

Po polsku

Nazwa jest przestarzała od 1.33, ale nadal działa — natkniesz się na nią w starym kodzie i w starszych polskich poradnikach, których w sieci jest na tyle mało, że trzymają się latami; kompilator wypisze ostrzeżenie deprecated, a nie błąd, i dlatego przykład na tej stronie musi je wyciszyć przez #![allow(deprecated)]. Poprawka jest czysto mechaniczna — wykreśl left, wpisz start — a powód zmiany warto zapamiętać: „lewo” opisuje położenie na ekranie, podczas gdy metoda opisuje położenie w bajtach, a dla pism pisanych od prawej do lewej te dwie rzeczy są sobie przeciwne.

Szukaj po polsku: przestarzała metoda w Ruscie · lewo a początek łańcucha · rust trim_left deprecated · rust trim_start