Having input data of two numbers, I need to construct either an iterator or a range and pass it further:
// ordering is not strictly defined
let x1: usize = 4;
let x2: usize = 2;
let mut x_range = ...;
if x1 < x2 {
x_range = (x1..=x2);
}
else {
// i.e if we have ((x1, x2) == (3,1) it should produce 3,2,1 sequence.
x_range = (x2..=x1).rev();
}
for x in x_range {
// do something
}
The problem is that two variants of ranges produced by regular range expression and rev()
have different types and throw type errors further in the code.
So I tried to define the range.
let mut x_range: std::ops::Range<usize>;
But received the error of = note: expected struct std::ops::Range<usize> found struct RangeInclusive<usize>
:
Then I tried to define an Iterator and then convert ranges with into_iter()
.
let mut x_range: dyn Iterator<usize>;
But then I received doesn't have a size known at compile-time
So is there any way to pass an abstract parent interface of an iterator or a range to other functions?