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 {...@@ -1391,12 +1391,19 @@ pub fn Aligned(comptime T: type, comptime alignment: ?mem.Alignment) type {
1391 return self.allocatedSlice()[self.items.len..];1391 return self.allocatedSlice()[self.items.len..];
1392 }1392 }
13931393
1394 /// Returns the last element from the list, or `null` if the list is empty.1394 /// Deprecated in favor of `last`.
1395 pub fn getLast(self: Self) ?T {1395 pub fn getLast(self: Self) ?T {
1396 if (self.items.len == 0) return null;1396 if (self.items.len == 0) return null;
1397 return self.items[self.items.len - 1];1397 return self.items[self.items.len - 1];
1398 }1398 }
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
1400 /// Called when memory growth is necessary. Returns a capacity larger than1407 /// Called when memory growth is necessary. Returns a capacity larger than
1401 /// minimum that grows super-linearly.1408 /// minimum that grows super-linearly.
1402 pub fn growCapacity(minimum: usize) usize {1409 pub fn growCapacity(minimum: usize) usize {
...@@ -2378,17 +2385,16 @@ test "Managed(?u32).pop()" {...@@ -2378,17 +2385,16 @@ test "Managed(?u32).pop()" {
2378 try testing.expect(list.pop() == null);2385 try testing.expect(list.pop() == null);
2379}2386}
23802387
2381test "Managed(u32).getLast()" {2388test "last" {
2382 const a = testing.allocator;2389 const a = testing.allocator;
23832390
2384 var list = Managed(u32).init(a);2391 var list: ArrayList(u32) = .empty;
2385 defer list.deinit();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);2396 try list.append(a, 2);
2390 const const_list = list;2397 try testing.expectEqual(list.last().?.*, 2);
2391 try testing.expectEqual(const_list.getLast().?, 2);
2392}2398}
23932399
2394test "return OutOfMemory when capacity would exceed maximum usize integer value" {2400test "return OutOfMemory when capacity would exceed maximum usize integer value" {