std-method

str::len

Measure the UTF-8 byte length, useful for byte budgets and protocol sizes. It is not a count of scalar values or grapheme clusters. Expected stdout (not executed): 8 4 Common mistake: Using len to enforce a user-visible character limit.

Formal Syntax & Structure

rust syntaxrust
fn len(&self) -> usize

Working Example

Example Snippetrust
fn main() {    let text = "سلام";    println!("{} {}", text.len(), text.chars().count());}
Rust Reference