primitive-type

Rust str: UTF-8 string slices

The str type represents UTF-8 text with a size that is not known statically. Most beginner code works with a borrowed string slice, &str, rather than a bare str value. Expected output: 8 Why: The four Persian letters in this literal each occupy two UTF-8 bytes. Common mistake: The len method counts bytes, not user-perceived characters; arbitrary byte slicing may split a UTF-8 sequence.

Formal Syntax & Structure

rust syntaxrust
let name: &str = "text";

Working Example

Example Snippetrust
fn main() {    let greeting: &str = "سلام";    println!("{}", greeting.len());}
Rust Reference