authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2026-05-21 17:29:02-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2026-05-25 18:54:36-07:00
logc678f94daa31f7ba8ea9f459eb4c442b02a8610c
tree346970ba1a1c26b193c1d5c3169876ca846415fe
parenta874e729df771c23576e8fcab9d58e45a20dc1bd

std.array_list: add last method, deprecated getLast


1 files changed, 14 insertions(+), 8 deletions(-)

lib/std/array_list.zig+14-8
......@@ -1391,12 +1391,19 @@ pub fn Aligned(comptime T: type, comptime alignment: ?mem.Alignment) type {
13911391 return self.allocatedSlice()[self.items.len..];
13921392 }
13931393
1394 /// Returns the last element from the list, or `null` if the list is empty.
1394 /// Deprecated in favor of `last`.
13951395 pub fn getLast(self: Self) ?T {
13961396 if (self.items.len == 0) return null;
13971397 return self.items[self.items.len - 1];
13981398 }
13991399
1400 /// Returns a pointer to the last element from the list, or `null` if
1401 /// the list is empty.
1402 pub fn last(self: Self) ?*T {
1403 if (self.items.len == 0) return null;
1404 return &self.items[self.items.len - 1];
1405 }
1406
14001407 /// Called when memory growth is necessary. Returns a capacity larger than
14011408 /// minimum that grows super-linearly.
14021409 pub fn growCapacity(minimum: usize) usize {
......@@ -2378,17 +2385,16 @@ test "Managed(?u32).pop()" {
23782385 try testing.expect(list.pop() == null);
23792386}
23802387
2381test "Managed(u32).getLast()" {
2388test "last" {
23822389 const a = testing.allocator;
23832390
2384 var list = Managed(u32).init(a);
2385 defer list.deinit();
2391 var list: ArrayList(u32) = .empty;
2392 defer list.deinit(a);
23862393
2387 try testing.expectEqual(list.getLast(), null);
2394 try testing.expectEqual(list.last(), null);
23882395
2389 try list.append(2);
2390 const const_list = list;
2391 try testing.expectEqual(const_list.getLast().?, 2);
2396 try list.append(a, 2);
2397 try testing.expectEqual(list.last().?.*, 2);
23922398}
23932399
23942400test "return OutOfMemory when capacity would exceed maximum usize integer value" {