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

Rust - string starts with

  • starts_with
fn main() {
    let names = [
        String::from("name.txt"),
        String::from("other"),
        String::from("narnia"),
    ];
    for name in names {
        print!("name: '{name}'");
        if name.starts_with("na") {
            print!(" starts with na");
        }
        println!();
    }
}
name: 'name.txt' starts with na
name: 'other'
name: 'narnia' starts with na