Skip to content

str::trim_start_matches

str methods · Strings

Level: reference · for working programmers

One line: Removes the pattern from the front as many times as it occurs"aaab".trim_start_matches('a') is "b".

pub fn trim_start_matches<P: Pattern>(&self, pat: P) -> &str

Stable since 1.30.0.

Repetition is the whole character of this method, and the reason it is not a substitute for strip_prefix. Stripping one ../ from a path is strip_prefix; removing every leading ../ is this.

Unlike trim_matches it accepts a multi-character &str, because it only searches forwards and needs no reverse searcher.

The two differ in what they tell you, too: this always returns a &str, so "there was nothing to remove" and "I removed three" look identical at the call site. If the presence of the prefix is information — a flag marker, a required sigil — use strip_prefix and read the Option.

Leading-zero removal is the classic use, and the classic edge case: "000".trim_start_matches('0') is "", not "0".

Example

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

fn main() {
    println!("{:?}", "aaab".trim_start_matches('a'));
    println!("{:?}", "../../src".trim_start_matches("../"));
    println!("{:?}", "0042".trim_start_matches('0'));

    // Repetition vs one occurrence.
    println!("{:?}", "../../src".strip_prefix("../"));

    // No match is silent here, and reported there.
    println!("{:?}", "src".trim_start_matches("../"));
    println!("{:?}", "src".strip_prefix("../"));

    // The all-zeros edge case.
    for n in ["0042", "0", "000", "40"] {
        let t = n.trim_start_matches('0');
        println!("{:<6} -> {:?}  (as a number: {})", format!("{n:?}"), t, if t.is_empty() { "0" } else { t });
    }
}

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

"b"
"src"
"42"
Some("../src")
"src"
None
"0042" -> "42"  (as a number: 42)
"0"    -> ""  (as a number: 0)
"000"  -> ""  (as a number: 0)
"40"   -> "40"  (as a number: 40)

See also

str::trim_start_matches in the standard library ↗

Po polsku

Powtarzalność jest tu całą treścią metody: trim_start_matches zdejmuje wzorzec z przodu tyle razy, ile razy on tam stoi, więc "../../src" daje "src", a nie "../src" — jeśli chodziło o usunięcie dokładnie jednego ../, to zadanie dla strip_prefix. W odróżnieniu od trim_matches przyjmuje wieloznakowy &str, bo szuka wyłącznie w przód i nie potrzebuje wyszukiwania wstecz. Zawsze zwraca &str, więc „nie było czego usuwać” i „usunąłem trzy wystąpienia” wyglądają w miejscu wywołania tak samo — widać to w przykładzie, gdzie "src".trim_start_matches("../") milczy, a "src".strip_prefix("../") mówi None. Klasyczne zastosowanie, czyli zdejmowanie wiodących zer z numerów kont czy kodów, ma też klasyczną pułapkę: "0" i "000" dają pusty łańcuch znaków, a nie "0", więc przed konwersją na liczbę ten przypadek trzeba obsłużyć osobno.

Szukaj po polsku: usuwanie wiodących zer · zdejmowanie przedrostka łańcucha · rust trim_start_matches vs strip_prefix · rust trim leading zeros