var fruits = ["Banana", "Orange", "Apple", "Mango"];
var index = fruits.indexOf("Apple");
let fruits = ["Banana", "Orange", "Apple", "Mango"];
let index = fruits.???
If there is no equivalent, maybe you can point me in the right direction? I found this example, but it's for vectors, not arrays.
You can use the method position
on any iterator. You can get an iterator over an array with the iter()
method. Try it like this:
let fruits = ["Banana", "Orange", "Apple", "Mango"];
let res1 = fruits.iter().position(|&s| s == "Apple");
let res2 = fruits.iter().position(|&s| s == "Peter");
println!("{:?}", res1); // outputs: Some(2)
println!("{:?}", res2); // outputs: None
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With