Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Length of string

  • len

  • With the len method we can get the length of a string in bytes.

fn main() {
    let text = String::from("Hello World!");
    println!("{text}");

    let length = text.len();
    println!("{length}");

    let text = String::from("👻👽👾");
    println!("{text}");

    let length = text.len();
    println!("{length}");
}
Hello World!
12
👻👽👾
12