push_mut 0.1.0

Push a value to the back of the vector, and return a mutable reference to it.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#![doc = include_str!("../README.md")]

/// An extension trait for `Vec<T>` that provides a [`PushMut::push_mut`] method.
pub trait PushMut<T> {
    /// Pushes a value to the back of the vector, and returns a mutable reference to it.
    fn push_mut(&mut self, value: T) -> &mut T;
}

impl<T> PushMut<T> for Vec<T> {
    fn push_mut(&mut self, value: T) -> &mut T {
        self.push(value);
        unsafe { self.last_mut().unwrap_unchecked() }
    }
}