I have a range, that I want to reverse if a condition is satisfied. Since for i in 0..9
will iterate the same way as for i in 9..0
simply swapping out the numbers will not work. Also (0..9).stepy_by(-1)
is not an option as .step_by()
only accepts a usize
. Therefore I tried so implement something like the following:
fn create_range(rev: bool) -> Range<usize> {
if rev {
0..9
} else {
(0..9).rev()
}
}
Which unfortunately also des not work since 0..9
returns a Range<usize>
but (0..9).rev()
returns a Rev<Range<usize>>
so the types do not match.
I ended up putting everything that I call within the loop in a function but I am not really satisfied with that.
if rev {
for i in (0..9).rev() {
do_stuff(i);
}
} else {
for i in 0..9 {
do_stuff(i);
}
}
The question is: Would it be simply possible to reverse a range if a condition is satisfied?