zig

fork of https://codeberg.org/ziglang/zig
Log | Files | Refs | README | LICENSE

commit c678f94daa31f7ba8ea9f459eb4c442b02a8610c (tree)
parent a874e729df771c23576e8fcab9d58e45a20dc1bd
Author: Andrew Kelley <andrew@ziglang.org>
Date:   Thu, 21 May 2026 17:29:02 -0700

std.array_list: add last method, deprecated getLast

Diffstat:
Mlib/std/array_list.zig | 22++++++++++++++--------
1 file changed, 14 insertions(+), 8 deletions(-)

diff --git a/lib/std/array_list.zig b/lib/std/array_list.zig @@ -1391,12 +1391,19 @@ pub fn Aligned(comptime T: type, comptime alignment: ?mem.Alignment) type { return self.allocatedSlice()[self.items.len..]; } - /// Returns the last element from the list, or `null` if the list is empty. + /// Deprecated in favor of `last`. pub fn getLast(self: Self) ?T { if (self.items.len == 0) return null; return self.items[self.items.len - 1]; } + /// Returns a pointer to the last element from the list, or `null` if + /// the list is empty. + pub fn last(self: Self) ?*T { + if (self.items.len == 0) return null; + return &self.items[self.items.len - 1]; + } + /// Called when memory growth is necessary. Returns a capacity larger than /// minimum that grows super-linearly. pub fn growCapacity(minimum: usize) usize { @@ -2378,17 +2385,16 @@ test "Managed(?u32).pop()" { try testing.expect(list.pop() == null); } -test "Managed(u32).getLast()" { +test "last" { const a = testing.allocator; - var list = Managed(u32).init(a); - defer list.deinit(); + var list: ArrayList(u32) = .empty; + defer list.deinit(a); - try testing.expectEqual(list.getLast(), null); + try testing.expectEqual(list.last(), null); - try list.append(2); - const const_list = list; - try testing.expectEqual(const_list.getLast().?, 2); + try list.append(a, 2); + try testing.expectEqual(list.last().?.*, 2); } test "return OutOfMemory when capacity would exceed maximum usize integer value" {