Library reference

Find text inside a string

Look for a piece of text anywhere in a longer string. These examples match the letters exactly.

str::contains

Simple example

Does abc contain b?

Start'abc' · 'b'
Resulttrue
main.rs
fn main() {
    println!("{}", "abc".contains("b"));
}
Output
true
Advertisements

Everyday examples

Does a ticket marked blue-zone include the word blue?

Start'blue-zone' · 'blue'
Resulttrue
main.rs
fn main() {
    println!("{}", "blue-zone".contains("blue"));
}
Output
true

Does a shelf marked fruit contain the word milk?

Start'fruit' · 'milk'
Resultfalse
main.rs
fn main() {
    println!("{}", "fruit".contains("milk"));
}
Output
false

Does the route name East Road include Road?

Start'East Road' · 'Road'
Resulttrue
main.rs
fn main() {
    println!("{}", "East Road".contains("Road"));
}
Output
true
Run the example
Terminal
rustc main.rs
./main
Official documentation