rustonaut / vec1

Rust `Vec<T>` wrapper that gurantees to contain at least 1 element
Apache License 2.0
90 stars 15 forks source link

Support moving the first or last element out of the vector #18

Closed robinkrahl closed 4 years ago

robinkrahl commented 4 years ago

As far as I see, the best way to move the first (or the last) element out of a Vec1 with the current implementation is something like v.into_vec().remove(0). It would be more convenient to have methods to extract the first and the last element, e. g.:

impl<T> Vec1<T> {
    pub fn split_off_first(self) -> (T, Vec<T>) {
        let mut vec = self.0;
        let first = vec.remove(0);
        (first, vec)
    }

    pub fn split_off_last(self) -> (Vec<T>, T) {
        let mut vec = self.0;
        let last = vec.remove(vec.len() - 1);
        (vec, last)
    }
}

Did I miss something? Would you accept a PR adding these two methods?

rustonaut commented 4 years ago

Seems good, if you want to do an PR feel free to do so, if not I will just add it soon anyway.