| ... | ... | @@ -482,14 +482,14 @@ pub fn ArrayListAligned(comptime T: type, comptime alignment: ?u29) type { |
| 482 | 482 | |
| 483 | 483 | /// Return the last element from the list. |
| 484 | 484 | /// Asserts the list has at least one item. |
| 485 | | pub fn getLast(self: *Self) T { |
| 485 | pub fn getLast(self: Self) T { |
| 486 | 486 | const val = self.items[self.items.len - 1]; |
| 487 | 487 | return val; |
| 488 | 488 | } |
| 489 | 489 | |
| 490 | 490 | /// Return the last element from the list, or |
| 491 | 491 | /// return `null` if list is empty. |
| 492 | | pub fn getLastOrNull(self: *Self) ?T { |
| 492 | pub fn getLastOrNull(self: Self) ?T { |
| 493 | 493 | if (self.items.len == 0) return null; |
| 494 | 494 | return self.getLast(); |
| 495 | 495 | } |
| ... | ... | @@ -961,14 +961,14 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ |
| 961 | 961 | |
| 962 | 962 | /// Return the last element from the list. |
| 963 | 963 | /// Asserts the list has at least one item. |
| 964 | | pub fn getLast(self: *Self) T { |
| 964 | pub fn getLast(self: Self) T { |
| 965 | 965 | const val = self.items[self.items.len - 1]; |
| 966 | 966 | return val; |
| 967 | 967 | } |
| 968 | 968 | |
| 969 | 969 | /// Return the last element from the list, or |
| 970 | 970 | /// return `null` if list is empty. |
| 971 | | pub fn getLastOrNull(self: *Self) ?T { |
| 971 | pub fn getLastOrNull(self: Self) ?T { |
| 972 | 972 | if (self.items.len == 0) return null; |
| 973 | 973 | return self.getLast(); |
| 974 | 974 | } |
| ... | ... | @@ -1719,3 +1719,27 @@ test "std.ArrayList(?u32).popOrNull()" { |
| 1719 | 1719 | try testing.expect(list.popOrNull().? == null); |
| 1720 | 1720 | try testing.expect(list.popOrNull() == null); |
| 1721 | 1721 | } |
| 1722 | |
| 1723 | test "std.ArrayList(u32).getLast()" { |
| 1724 | const a = testing.allocator; |
| 1725 | |
| 1726 | var list = ArrayList(u32).init(a); |
| 1727 | defer list.deinit(); |
| 1728 | |
| 1729 | try list.append(2); |
| 1730 | const const_list = list; |
| 1731 | try testing.expectEqual(const_list.getLast(), 2); |
| 1732 | } |
| 1733 | |
| 1734 | test "std.ArrayList(u32).getLastOrNull()" { |
| 1735 | const a = testing.allocator; |
| 1736 | |
| 1737 | var list = ArrayList(u32).init(a); |
| 1738 | defer list.deinit(); |
| 1739 | |
| 1740 | try testing.expectEqual(list.getLastOrNull(), null); |
| 1741 | |
| 1742 | try list.append(2); |
| 1743 | const const_list = list; |
| 1744 | try testing.expectEqual(const_list.getLastOrNull().?, 2); |
| 1745 | } |