-2

In rust, how do I iterate all over the all but the last n elements?

[0, 1, 2, 3, 4].iter().???(n)


Similar but not the same as Use all but the last element from an iterator which is actually about matching chunk sizes. The content of that Question is "To be sure that all parts have equal length, I just want to drop that last element and then call map() on what's left.". All the proceeding answers use variations of chunk in their sample code. Reviewing those code samples, I found them too difficult to apply to my needs. Hence this new question with my Answer that does not use chunk.

Chayim Friedman
  • 47,971
  • 5
  • 48
  • 77
JamesThomasMoon
  • 6,169
  • 7
  • 37
  • 63
  • 2
    In how far does the linked Q&A not suffice, especially the accepted answers suggestion to peek at the next item? – MisterMiyagi Jan 05 '23 at 07:39
  • "which is actually about matching chunk sizes." the accepted answer from shepmaster literally solves your problem as-is (as it answers the problem as titled), it never even looks at chunk sizes. The second and not-accepted answer filters based on chunk sizes. – Masklinn Jan 05 '23 at 11:38
  • @MisterMiyagi The linked question is regarding matching chunk sizes but has a misleading title. This question is not about chunk sizes. This question is not a duplicate. – JamesThomasMoon Jan 06 '23 at 04:46
  • @Masklinn you wrote "_it never even looks at chunk sizes._" referring to this Answer (https://stackoverflow.com/a/48103219/471376). That Answer has with `chunks(2)` in the code. – JamesThomasMoon Jan 06 '23 at 04:48
  • The chunks are just the incidental iterable of that question. If you think a generic iterable answer is needed, why not post it there? – MisterMiyagi Jan 06 '23 at 07:51
  • @MisterMiyagi "_If you think a generic iterable answer is needed, why not post it there?_" These are different questions with overlapping but different solutions/Answers. The other question explicitly requests "_To be sure that all parts have equal length, I just want to drop that last element and then call map() on what's left._". That is a different problem than the Question here "_iterate all over the all but the last n elements?_". That is why the Answers for the other Question all involve using `chunk`. Your insistence on downvoting this question is frustrating. – JamesThomasMoon Jan 07 '23 at 00:16
  • @JamesThomasMoon I haven’t downvoted this question (and didn’t even close ). But that you think so is certainly a sign that I should stop trying to work with you on salvaging this. – MisterMiyagi Jan 07 '23 at 08:35
  • @MisterMiyagi oh I'm sorry, I had presumed you had. Just feeling a little frustrated. Thanks for your feedback. – JamesThomasMoon Jan 07 '23 at 21:14

2 Answers2

1

Either:

array[..array.len() - n].iter()
array.iter().take(array.len() - n)
array.iter().rev().skip(n).rev()
Miiao
  • 751
  • 1
  • 8
-3

tl;dr take the len - n iterations

fn print_elements_not_last_n(elems: &[i32], n: usize) {
    for elem in elems.iter().take(elems.len() - n) {
        println!("{:?}", elem);
    }
}

fn main() {
    let elems: [i32; 5] = [0, 1, 2, 3, 4];

    print_elements_not_last_n(&elems, 1);
    println!();
    print_elements_not_last_n(&elems, 2);
}

This prints

0
1
2
3

0
1
2

You can also use iter().count() instead of len. Using count might be necessary for code that processes an Iterable instead of a Sequence.

JamesThomasMoon
  • 6,169
  • 7
  • 37
  • 63