Library reference

Find an item in a list

Check whether a particular value appears anywhere in a list. A single match is enough.

slice::contains

Simple example

Does the list contain two?

Start1 · 2 · 3
Resulttrue
main.rs
fn main() {
    let values = [1, 2, 3];
    println!("{}", values.contains(&2));
}
Output
true
Advertisements

Everyday examples

Is ticket 17 on the pickup list?

Start15 · 17 · 20
Resulttrue
main.rs
fn main() {
    let values = [15, 17, 20];
    println!("{}", values.contains(&17));
}
Output
true

Does this bus stop at stop 6?

Start2 · 4 · 8
Resultfalse
main.rs
fn main() {
    let values = [2, 4, 8];
    println!("{}", values.contains(&6));
}
Output
false

Is parcel 42 on the van?

Start42 · 18 · 15
Resulttrue
main.rs
fn main() {
    let values = [42, 18, 15];
    println!("{}", values.contains(&42));
}
Output
true
Run the example
Terminal
rustc main.rs
./main
Official documentation