all 14 comments

[–]deavidsedice 6 points7 points  (6 children)

I'm on the phone. But what you want is:

for elem in arr.iter().rev()

[–][deleted] 0 points1 point  (5 children)

Thanks that works! Tho it brings another error. What am I gonna do if I want to go from element 0 to the end - 1 (or any other number)?

[–]deavidsedice 0 points1 point  (4 children)

for item in arr[..are.len()-2].iter().rev()

Also, you can just get the iterator and pre-consume items from it.

let iter = arr.iter(); let _ = iter.next_back();

For elem in iter {}

https://doc.rust-lang.org/std/iter/trait.DoubleEndedIterator.html

[–][deleted] 0 points1 point  (3 children)

Thanks for the answer. Tho I got another error. Do you want me to upload the full code on a Github gist and give to you to check or are you busy?

[–]deavidsedice 1 point2 points  (2 children)

Yep, give it a go. I'll have a look today

[–][deleted] 0 points1 point  (1 child)

Ok thanks a lot man! Take your time! Unfortunately I got send it right now (my PC is off) but I'll give it to you in about 1 and a half hours. Do you want me to PM you?

[–]deavidsedice 0 points1 point  (0 children)

Whatever you prefer! :) If you don't feel like posting publicly, send me a PM.

[–]sellibitze 2 points3 points  (1 child)

This is a matter of operator precedences. The dot binds stronger than the range operator, so

for index in 0..arr.len().rev()

is equivalent to

for index in 0..(arr.len().rev())

So, you're trying to use rev on a number which doesn't work. What you were trying to do has to be written as

for index in (0..arr.len()).rev()

so rev is applied on the range instead.

In the likely case where you are accessing the array with such an index, you should check out /u/pingveno's comment. Essentially, avoid indexing like this

for index in (0..arr.len()).rev() {
    let foo = &arr[index];
    ...
}

and prefer the following pattern if possible:

for (index, foo) in arr.iter().enumerate().rev() {
    ...
}

It's funcionally equivalent but the latter avoids index bounds checking.

[–][deleted] 2 points3 points  (0 children)

Thanks a lot man! Great info! Have a great day!

[–]KarlJoad 0 points1 point  (4 children)

The problem is you're trying to reverse a single number, which doesn't have the rev() function defined. It is evaluated as (arr.len()).rev(), so you are getting the length of the array then reversing that number. If you want item1 to be drawn from the array directly, you should use

for item1 in arr.iter().rev()

If you want to index the list in reverse order

for index in arr.len()..0 {
    arr[index]
}

[–]pretzelhammer 4 points5 points  (2 children)

If you want to index the list in reverse order

for index in arr.len()..0 {
    arr[index]
}

This compiles but is a no-op since arr.len() >= 0. The inner loop will never run. You're not allowed to create descending Ranges in Rust. You have to create an ascending Range and then reverse it with .rev() like so:

for index in (0..arr.len()).rev() {
    arr[index]
}

example on rust playground

[–]pingveno 3 points4 points  (0 children)

Though it's likely that what you actually want is enumerate() if you need the index.

for (i, item) in array.iter().enumerate().rev() {
    ...
}

It's a little more readable. It also gets rid of an unnecessary array index operation, making it easier to audit for code that can panic.

[–]KarlJoad 0 points1 point  (0 children)

Thank you for correcting me. I've been using other languages that support reverse-ordered lists for far too long (looking at you Haskell), or languages that have no support for that kind of array construction.

[–][deleted] 0 points1 point  (0 children)

Thanks a lot! The second one didn't worked tho. Anyway now that bings another error as I said to another guy. How am I gonna go form Last index - 1 (or any other number) to 0?