i don't love this code
for i in (1..10).rev(){
println!("{}",i);
}
why we can't use this cleaner code in rust-lang
for i in 10..1{
println!("{}",i);
}
If we think about this mathematically..
The range 10..1 can be seen as the set formed by: { x | is_integer(x) && 10 <= x < 1 }.
This holds for exactly zero integers, wherefore the set is empty.
It is important to not have too surprising and different semantics than mathematics.
I think that the hassle of .rev() is not too large and well worth this consistency.
@mehrati Because it would be surprising in cases that aren't literals.
For example, today the following code is fine:
for i in 5..v.len() { println!("{}", v[i]); }
If the container is smaller than 5 elements, the loop doesn't run. If it started iterating the other way instead, it would panic with out-of-range, which would be both confusing and a breaking change.
Triaging this issue:
@mehrati: is the given explanation sufficient for you? can we close this?
yes very thank you fellow-citizen 鬲
no problem you can close this
Thanks =)
Most helpful comment
If we think about this mathematically..
The range
10..1can be seen as the set formed by:{ x | is_integer(x) && 10 <= x < 1 }.This holds for exactly zero integers, wherefore the set is empty.
It is important to not have too surprising and different semantics than mathematics.
I think that the hassle of
.rev()is not too large and well worth this consistency.