[][src]Trait stdext::vec::VecExt

pub trait VecExt<T> {
    fn resize_up_with<F>(&mut self, new_len: usize, f: F)
    where
        F: FnMut() -> T
; }

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

Required methods

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.
Loading content...

Implementations on Foreign Types

impl<T> VecExt<T> for Vec<T>[src]

Loading content...

Implementors

Loading content...