Methods to pop from a slice

I've regularly written code where the following methods would be useful:

impl<T> [T] { // also mut versions
    fn pop_front(&mut &self) -> Option<&T>;

    fn pop_front_chunk<const N: usize>(&mut &self) -> Option<&[T; N]>;

    fn pop_back(&mut &self) -> Option<&T>;

    fn pop_back_chunk<const N: usize>(&mut &self) -> Option<&[T; N]>;
}

Each of these methods acting like the corresponding split_first/split_last methods, except instead of returning a tuple they modify self in-place to remove the returned value. So e.g. calling pop_front in a loop would return values equivalently to constructing an iterator and

Would anyone else find methods like these useful?

Sounds like https://doc.rust-lang.org/std/primitive.slice.html#method.take_first and such?

They've been around for like 5 years, but still aren't stable, so I don't know if that means that libs-api isn't a fan.

It probably just means that nobody has proposed stabilizing them. Which would suggest either there's no great call for them or else they're not particularly discoverable.

On discoverability: if I needed to "pop" form a slice my first thought would be to reach for slice patterns, not to look for a method.

2 Likes