Trait stdext::vec::VecExt

source ·
pub trait VecExt<T> {
    // Required methods
    fn resize_up_with<F>(&mut self, new_len: usize, f: F)
       where F: FnMut() -> T;
    fn remove_item<V>(&mut self, item: &V) -> Option<T>
       where T: PartialEq<V>;
}
Expand description

Extension trait with useful methods for std::vec::Vec.

Required Methods§

source

fn resize_up_with<F>(&mut self, new_len: usize, f: F)
where F: FnMut() -> T,

Resizes the Vec in-place if the provided new_len is greater than the current Vec length.

In simple words, this method only make vector bigger, but not smaller. Calling this method with a length smaller or equal to the current length will do nothing.

This method uses a closure to create new values on every push. If you’d rather Clone a given value, use resize_up. If you want to use the Default trait to generate values, you can pass Default::default() as the second argument.

§Examples
use stdext::prelude::*;

let mut vec = vec![1, 2, 3];
vec.resize_up_with(5, Default::default);
assert_eq!(vec, [1, 2, 3, 0, 0]);

let mut vec = vec![];
let mut p = 1;
vec.resize_up_with(4, || { p *= 2; p });
assert_eq!(vec, [2, 4, 8, 16]);

let mut vec = vec![1, 2, 3];
vec.resize_up_with(1, Default::default);
assert_eq!(vec, [1, 2, 3]); // Resizing to smaller size does nothing.
source

fn remove_item<V>(&mut self, item: &V) -> Option<T>
where T: PartialEq<V>,

Removes the first instance of item from the vector if the item exists.

Based on the unstable vec_remove_item feature, which has been deprecated and will be removed.

§Examples
use stdext::prelude::*;
let mut vec = vec![1, 2, 3, 1];

vec.remove_item(&1);

assert_eq!(vec, vec![2, 3, 1]);

Object Safety§

This trait is not object safe.

Implementations on Foreign Types§

source§

impl<T> VecExt<T> for Vec<T>

source§

fn resize_up_with<F>(&mut self, new_len: usize, f: F)
where F: FnMut() -> T,

source§

fn remove_item<V>(&mut self, item: &V) -> Option<T>
where T: PartialEq<V>,

Implementors§