How do I find the index of an element in an array, vector or slice?

I think you should look at the position method instead.

fn main() {
    let test = vec!["one", "two", "three"];
    let index = test.iter().position(|&r| r == "two").unwrap();
    println!("{}", index);
}

You can test it here.

Note that this works for any iterator, so it can be used for vectors, arrays, and slices, all of which produce iterators.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)