I am trying to build an Iterator
in Rust that returns the values and indexes of non-null elements in an array. This Iterator
should be reversible based on a bool
parameter.
I managed to build the Iterator
without the reverse condition :
fn main() {
let arr: [u8; 8] = [0, 2, 0, 0, 0, 1, 0, 5];
for (i, x) in arr.iter().enumerate().filter(|(_, &x)| x!=0) {
println!("index: {}, value: {}", i, x);
}
}
>>> index: 1, value: 2
>>> index: 5, value: 1
>>> index: 7, value: 5
However, when trying to build an Iterator
based on a boolean condition (based on this post), I get an error when compiling :
temporary value dropped while borrowed
consider using a let
binding to create a longer lived value rustc(E0716)
.
fn main() {
// Array to observe
let arr: [u8; 8] = [0, 2, 0, 0, 0, 1, 0, 5];
// Reverse parameter
let reverse: bool = true;
// Building Iterator based on `reverse` parameter
let iter: &mut dyn Iterator<Item = (usize, &u8)> = match reverse {
// forward Iterator
false => {
&mut arr.iter().enumerate().filter(|(_, &x)| x!=0)
}
// Reversed iterator
true => {
&mut arr.iter().enumerate().filter(|(_, &x)| x!=0).rev()
}
};
// Print indices and values of non 0 elements in the array
for (i, x) in iter {
println!("index: {}, value: {}", i, x);
}
}
I have tried cloning the array, or declaring the Iterator
with the let
keyword as suggested by the compiler, but none of it seemed to work. Any idea?