You will encounter the filter() method when working with Rust vectors. This method allows us to create a new vector from another vector but only with the elements that meet a specified predicate.
The method takes a closure that tests each element in the input vector and returns a Boolean value which indicates whether the element should be included in the filtered vector.
In this tutorial, we will explore how to work with this method and how we can use it to filter the vectors and store the resulting values in a new vector.
Rust Filter Method
The following snippet shows the syntax of the filter() method:
where
F: FnMut(&Self::Item) -> bool,
The method returns an iterator adapter of Filter type which we can be used to create a new iterator over the elements in the input vector that matches the specified predicate.
Example Demonstration 1:
Let us look at an example on how we can use this method. Suppose we have a vector of numbers that we wish to create a new vector with the elements that match a specific condition. For example, we can filter only the even integers from the input vector and store them in a new vector.
We can do this by calling the filter() method and passing a closure that checks for even numbers to the method as shown in the following:
let vector = vec![1, 2, 3, 4, 5, 6,7,8,9];
let even_vect = vector.iter().filter(|&n| n % 2 == 0).collect::<Vec<_>>();
println!("{:?}", even_vect);
}
In this example, we start by creating a vector of integer values using the vec! macro. We then call the filter method on the vector which creates an iterator over its elements.
Using the filter() method on the iterator, we create a new iterator that only includes the elements that meet the specified predicate. In this case, the predicate checks if the element is even.
We then collect the filtered iterator into a new vector using the collect() method.
Running the previous code should return the following:
NOTE: In the provided example, we use the ::Vec<_>> syntax to specify the type of vector that we wish to create. The “_” represents the placeholder that tells Rust to infer the vector type from the elements that are being collected.
Example Demonstration 2:
We can also filter a vector of other types such as strings. An example is as follows:
let names = vec!["Alice", "Bob", "Josephine", "Jasmine", "James"];
let j_names = names.iter().filter(|&n| n.starts_with("J")).collect::<Vec<_>>();
println!("{:?}", j_names);
}
In this example, we start by creating a vector of names. We then call the iter() method on the vector, creating an iterator over its elements. Next, using the filter() method, we take this iterator and create a new iterator that only includes the elements that match the specified predicate. In this case, we are interested in the elements that start with the letter “J.”
Finally, we collect the filtered iterator into a new vector and print it to the console. The resulting values are as follows:
There you have it!
Conclusion
This post explored the fundamentals of working with the filter() method to create a new vector with elements that match a specific predicate. The filter() method is advantageous when working with Rust vectors. It can quickly help you filter the unwanted elements without manually sorting or iterating over the vector.