I want to transform this function to operate on an iterable container of i32
:
fn double_positives0<'a>(numbers: &'a Vec<i32>) -> impl Iterator<Item = i32> + 'a {
numbers.iter().filter(|x| x > &&0).map(|x| x * 2)
}
I made a too-generic form:
fn double_positives1<T>(
numbers: T,
min: T::Item,
v: T::Item,
) -> impl Iterator<Item = <T::Item as std::ops::Mul<T::Item>>::Output>
where
T: IntoIterator,
T::Item: PartialOrd,
T::Item: Mul<T::Item>,
T::Item: Copy,
{
numbers
.into_iter()
.filter(move |x| x > &min)
.map(move |x| x * v)
}
I wasn't able to write a function
- Which uses reference input (as in
double_positives0
) - Restricted to
T::Item
of typei32
(this could simplifywhere
)
Something like
fn double_positives2<'a, T>(numbers: &'a T, min: i32, v: i32) -> impl Iterator<Item = i32> + 'a
where
T: IntoIterator,
T::Item: I32,
{
unimplemented!()
}
Rust playground with the previous code samples
How can I do this?