# Looping through BoundedArray slice in reverse

**URL:** <https://ziggit.dev/t/looping-through-boundedarray-slice-in-reverse/6963>\
**Category:** Help\
**Tags:** language\
**Created:** [November 22, 2024, 12:13pm UTC](https://ziggit.dev/t/looping-through-boundedarray-slice-in-reverse/6963 "2024-11-22T12:13:25Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![ericlang](https://ziggit.dev/user_avatar/ziggit.dev/ericlang/32/8515_2.png) [@ericlang](https://ziggit.dev/u/ericlang)\
**Post date:** [November 22, 2024, 12:13pm UTC](https://ziggit.dev/t/looping-through-boundedarray-slice-in-reverse/6963/1 "2024-11-22T12:13:25Z")

</div>

We can loop through a bounded array slice (ar) with:  
`for (ar.slice())`  
but is there a neat way to go backwards instead of indexing myself?

---

<div class="post-metadata">

**Author:** ![jmc](https://ziggit.dev/user_avatar/ziggit.dev/jmc/32/38_2.png) [@jmc](https://ziggit.dev/u/jmc)\
**Post date:** [November 22, 2024, 1:31pm UTC](https://ziggit.dev/t/looping-through-boundedarray-slice-in-reverse/6963/2 "2024-11-22T13:31:14Z")

</div>

I don’t think there is, at least not using a `for` loop. You could write your own iterator to go in reverse and use that in a `while` loop:

```zig
$ cat main.zig 
const std = @import("std");

fn ReverseIterator(comptime Slice: type) type {
    const Elem = std.meta.Elem(Slice);
    return struct {
        position: usize,
        slice: Slice,

        pub fn next(self: *@This()) ?Elem {
            if (self.position >= self.slice.len) return null;
            const elem = self.slice[self.slice.len - 1 - self.position];
            self.position += 1;
            return elem;
        }
    };
}

fn reverse(slice: anytype) ReverseIterator(@TypeOf(slice)) {
    return ReverseIterator(@TypeOf(slice)){
        .position = 0,
        .slice = slice,
    };
}

pub fn main() !void {
    var a = try std.BoundedArray(u8, 128).init(3);
    a.appendSliceAssumeCapacity(&.{ 1, 2, 3 });

    const s = a.constSlice();
    std.debug.print("slice length={d}\n", .{s.len});

    var it = reverse(s);
    while (it.next()) |n| {
        std.debug.print(" - {d}\n", .{n});
    }
}

$ zig run main.zig
slice length=6
 - 3
 - 2
 - 1
 - 0
 - 0
 - 0

```

---

<div class="post-metadata">

**Author:** ![7R35C0](https://ziggit.dev/user_avatar/ziggit.dev/7r35c0/32/2393_2.png) [@7R35C0](https://ziggit.dev/u/7R35C0)\
**Post date:** [November 22, 2024, 2:49pm UTC](https://ziggit.dev/t/looping-through-boundedarray-slice-in-reverse/6963/3 "2024-11-22T14:49:25Z")

</div>

See one of these links, maybe it suits your needs:

- In-place order reversal of a slice [std.mem.reverse](https://ziglang.org/documentation/master/std/#std.mem.reverse)
- Iterates over a slice in reverse [std.mem.reverseIterator](https://ziglang.org/documentation/master/std/#std.mem.reverseIterator)

---

<div class="post-metadata">

**Author:** ![jmc](https://ziggit.dev/user_avatar/ziggit.dev/jmc/32/38_2.png) [@jmc](https://ziggit.dev/u/jmc)\
**Post date:** [November 22, 2024, 5:35pm UTC](https://ziggit.dev/t/looping-through-boundedarray-slice-in-reverse/6963/4 "2024-11-22T17:35:51Z")

</div>

Are these new? I missed the existence of `std.mem.reverseIterator()` completely… Well, go use that one, it’s better than my toy version above 😇

---

<div class="post-metadata">

**Author:** ![ericlang](https://ziggit.dev/user_avatar/ziggit.dev/ericlang/32/8515_2.png) [@ericlang](https://ziggit.dev/u/ericlang)\
**Post date:** [November 22, 2024, 6:23pm UTC](https://ziggit.dev/t/looping-through-boundedarray-slice-in-reverse/6963/5 "2024-11-22T18:23:23Z")

</div>

I will have a look. Thanks!  
std.mem is full of surprises.

---

<div class="post-metadata">

**Author:** ![dimdin](https://ziggit.dev/user_avatar/ziggit.dev/dimdin/32/1457_2.png) [@dimdin](https://ziggit.dev/u/dimdin)\
**Post date:** [November 22, 2024, 7:21pm UTC](https://ziggit.dev/t/looping-through-boundedarray-slice-in-reverse/6963/6 "2024-11-22T19:21:48Z")

</div>

> [@jmc](#):
>
> Are these new?

There are available since [0.11.0](https://ziglang.org/download/0.11.0/release-notes.html#Strings)

---

<div class="post-metadata">

**Author:** ![7R35C0](https://ziggit.dev/user_avatar/ziggit.dev/7r35c0/32/2393_2.png) [@7R35C0](https://ziggit.dev/u/7R35C0)\
**Post date:** [November 23, 2024, 8:30am UTC](https://ziggit.dev/t/looping-through-boundedarray-slice-in-reverse/6963/7 "2024-11-23T08:30:41Z")

</div>

I don’t know how long they’ve been around, I just remembered them.
