Rfcs: reverse iterator

Created on 28 Dec 2017  路  5Comments  路  Source: rust-lang/rfcs

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); }

T-libs

Most helpful comment

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.

All 5 comments

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 =)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

onelson picture onelson  路  3Comments

mqudsi picture mqudsi  路  3Comments

camden-smallwood-zz picture camden-smallwood-zz  路  3Comments

rudolfschmidt picture rudolfschmidt  路  3Comments

rust-highfive picture rust-highfive  路  4Comments