Skip to content

str::trim_right

str methods · Strings

Level: reference · for working programmers

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

pub fn trim_right(&self) -> &str

Stable since 1.0.0.

Renamed alongside trim_left, for the same reason: the end of a string is a byte position, not a screen position.

Identical behaviour — delete right, write end.

Example

str_trim_right.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_right());
    println!("{:?}", s.trim_end());
    println!("identical: {}", s.trim_right() == s.trim_end());
}

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

"  padded"
"  padded"
identical: true

See also

str::trim_right in the standard library ↗

Po polsku

Jeśli przychodzisz od SQL-owego RTRIM albo pythonowego rstrip, trim_right jest nazwą, po którą sięgniesz odruchowo — i właśnie dlatego biblioteka standardowa oznaczyła ją w wersji 1.33 jako przestarzałą (deprecated) na rzecz trim_end. Powód wart zapamiętania: „prawa strona” to pojęcie ekranu, a nie danych — w piśmie od prawej do lewej (arabskim, hebrajskim) koniec łańcucha znaków wypada wizualnie po lewej, więc end nazywa pozycję bajtową i nie kłamie niezależnie od pisma. Działanie obu metod jest identyczne — przykład drukuje identical: true — a stara nazwa nadal się kompiluje, tylko z ostrzeżeniem, które #![allow(deprecated)] na górze pliku wycisza. W nowym kodzie po prostu zamień right na end.

Szukaj po polsku: obcinanie białych znaków z prawej · metody przestarzałe w Ruscie · rust trim_right deprecated · rust trim_end