Skip to content

str::trim_right_matches

str methods · Strings

Level: reference · for working programmers

Deprecated since Rust 1.33 — use str::trim_end_matches 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_end_matches.

pub fn trim_right_matches<P: Pattern>(&self, pat: P) -> &str
where
    for<'a> P::Searcher<'a>: ReverseSearcher<'a>,

Same rename, same reason. Behaviour is unchanged: the pattern is removed from the back repeatedly.

Example

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

#![allow(deprecated)]

fn main() {
    let s = "value///";
    println!("{:?}", s.trim_right_matches('/'));
    println!("{:?}", s.trim_end_matches('/'));
    println!("identical: {}", s.trim_right_matches('/') == s.trim_end_matches('/'));
}

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

"value"
"value"
identical: true

See also

str::trim_right_matches in the standard library ↗

Po polsku

Ta metoda dzieli los całej czwórki *_left / *_right: od wersji 1.33 nazywa się trim_end_matches i tylko tak powinna trafiać do nowego kodu. Przy okazji warto zobaczyć, co robi sam mechanizm, bo nazwa tego nie zdradza — wzorzec jest zdejmowany z końca wielokrotnie, więc "value///" daje "value", a nie "value//"; przy normalizowaniu ścieżek albo adresów URL zwykle dokładnie o to chodzi. Jeśli natomiast chcesz usunąć jedno wystąpienie i jeszcze wiedzieć, czy w ogóle tam było, sięgnij po strip_suffix, która zwraca Option.

Szukaj po polsku: usuwanie ukośników z końca ścieżki · normalizacja adresu URL w Ruscie · rust trim_right_matches deprecated · rust trim_end_matches vs strip_suffix