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

Is one string in another string - contains?

  • contains

  • find

  • in

  • index

  • contains will return a boolean value telling if one string contains the other

  • find will return a number indicating the location of a substring (or None if the string cannot be found).

fn main() {
    let text = "The black cat climed the green tree";
    println!("{}", text.contains("cat"));
    println!("{}", text.contains("dog"));
    println!("{}", text.find("cat").unwrap());

    // println!("{}", text.find("dog").unwrap()); // panics

    println!("{}", text.find('a').unwrap());
    println!("{}", text[7..].find('a').unwrap());
}
true
false
10
6
4