To check if a specific item is present in a vector in Rust, you can use the contains()
method.
In this example,
vec
containing numeric elements.contains()
method on vec
to check if the specified item is present.fn main() {
let vec = vec![1, 2, 3, 4, 5];
let item = 3;
let result = vec.contains(&item);
println!("{:?}", result);
}
true
In this example,
vec
containing string elements.contains()
method on vec
to check if the specified item is present.fn main() {
let vec = vec!["apple", "banana", "cherry", "date"];
let item = "banana";
let result = vec.contains(&item);
println!("{:?}", result);
}
true
In this tutorial, we learned How to check if a Specific Item is present in the Vector in Rust language with well detailed examples.