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 ends with

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