Iterators are a powerful abstraction in Rust, enabling developers to process sequences of data in a functional, efficient, and composable way. They are the foundation of many collection-related operations and provide a clean and expressive alternative to traditional loops.
In this challenge, you will implement two functions that utilize iterators to perform specific operations:
Using Rust's iterator methods such as filter
, map
, and collect
, you'll create concise, functional implementations that demonstrate the elegance and power of iterators.
Write the following two functions:
filter_even_numbers
:
Takes an iterator of integers and filters out even numbers, returning a vector of the remaining odd numbers.
uppercase_strings
:
Takes an iterator of string slices and converts each string to uppercase, returning a vector of the transformed strings.
Read the main()
function carefully to understand how the functions will be used and what is expected from them.
Use the filter
method for filtering elements based on a condition.
Make sure you dereference the values when using them in the closure. For example:
Or
Use the collect
method to gather results from an iterator into a vector.
Use the map
method for transforming elements in an iterator.
Use the to_uppercase
method to convert a string to uppercase.
Define lifetimes for the input and output types in the function signature.