builtin: add arr.pop_left() func (#25133)

This commit is contained in:
kbkpbot 2025-08-19 19:37:18 +08:00 committed by GitHub
parent b85782b7e7
commit a8187442b3
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
11 changed files with 110 additions and 9 deletions

View file

@ -1197,6 +1197,40 @@ fn test_reverse_in_place() {
assert c == [[5, 6], [3, 4], [1, 2]]
}
fn test_array_int_pop_left() {
mut a := [1, 2, 3, 4, 5]
b := unsafe { a[..5] } // full slice view
assert a.len == 5
first := a[0]
x := a.pop_left()
assert first == x
assert a.len == 4
assert a.cap == 4
y := a.pop_left()
assert y == 2
a[0] = 100
// NOTE: update a[0] also update b[2]
assert b == [1, 2, 100, 4, 5]
mut one_elem := [1]
one := one_elem.pop_left()
assert one_elem.len == 0
assert one_elem.cap == 0
assert one == 1
}
fn test_array_string_pop_left() {
mut a := ['abc', 'def', 'xyz']
assert a.len == 3
x := a.first()
y := a.pop_left()
assert x == y
assert a.pop_left() == 'def'
assert a.pop_left() == 'xyz'
assert a.len == 0
assert a.cap == 0
}
fn test_array_int_pop() {
mut a := [1, 2, 3, 4, 5]
assert a.len == 5