std-method

String::truncate

Shorten to the requested byte length if it is smaller than the current length. A shorter length must be a scalar boundary; a larger length does not extend the string. Expected stdout (not executed): Aش Common mistake: Truncating at an arbitrary byte count within a letter.

Formal Syntax & Structure

rust syntaxrust
fn truncate(&mut self, new_len: usize)

Working Example

Example Snippetrust
fn main() {    let mut label = String::from("AشB");    label.truncate(3);    println!("{label}");}
Rust Reference