Function reverseIterator [src]

Iterates over a slice in reverse.

Prototype

pub fn reverseIterator(slice: anytype) ReverseIterator(@TypeOf(slice))

Example

test reverseIterator { { var it = reverseIterator("abc"); try testing.expectEqual(@as(?u8, 'c'), it.next()); try testing.expectEqual(@as(?u8, 'b'), it.next()); try testing.expectEqual(@as(?u8, 'a'), it.next()); try testing.expectEqual(@as(?u8, null), it.next()); } { var array = [2]i32{ 3, 7 }; const slice: []const i32 = &array; var it = reverseIterator(slice); try testing.expectEqual(@as(?i32, 7), it.next()); try testing.expectEqual(@as(?i32, 3), it.next()); try testing.expectEqual(@as(?i32, null), it.next()); it = reverseIterator(slice); try testing.expect(*const i32 == @TypeOf(it.nextPtr().?)); try testing.expectEqual(@as(?i32, 7), it.nextPtr().?.*); try testing.expectEqual(@as(?i32, 3), it.nextPtr().?.*); try testing.expectEqual(@as(?*const i32, null), it.nextPtr()); const mut_slice: []i32 = &array; var mut_it = reverseIterator(mut_slice); mut_it.nextPtr().?.* += 1; mut_it.nextPtr().?.* += 2; try testing.expectEqual([2]i32{ 5, 8 }, array); } { var array = [2]i32{ 3, 7 }; const ptr_to_array: *const [2]i32 = &array; var it = reverseIterator(ptr_to_array); try testing.expectEqual(@as(?i32, 7), it.next()); try testing.expectEqual(@as(?i32, 3), it.next()); try testing.expectEqual(@as(?i32, null), it.next()); it = reverseIterator(ptr_to_array); try testing.expect(*const i32 == @TypeOf(it.nextPtr().?)); try testing.expectEqual(@as(?i32, 7), it.nextPtr().?.*); try testing.expectEqual(@as(?i32, 3), it.nextPtr().?.*); try testing.expectEqual(@as(?*const i32, null), it.nextPtr()); const mut_ptr_to_array: *[2]i32 = &array; var mut_it = reverseIterator(mut_ptr_to_array); mut_it.nextPtr().?.* += 1; mut_it.nextPtr().?.* += 2; try testing.expectEqual([2]i32{ 5, 8 }, array); } }

Source

pub fn reverseIterator(slice: anytype) ReverseIterator(@TypeOf(slice)) { return .{ .ptr = slice.ptr, .index = slice.len }; }