Skip to content

Vec::resize

Vec methods · Collections

Level: reference · for working programmers

One line: Set the length to new_len, padding with clones of a value.

pub fn resize(&mut self, new_len: usize, value: T)

Stable since 1.5.0.

Grows by cloning the value into each new slot; shrinks by dropping the tail, in which case the value is ignored. Equal length does nothing.

T: Clone, and it really clones — one value in, n independent copies out.

The common use is padding a buffer to a fixed width: record.resize(8, b'.').

When the padding must be a fresh value each time rather than a clone of one prototype — a Vec::new() per row, a counter, anything not Clone — use resize_with, which calls a closure per slot.

For shrinking alone, truncate says it more directly and needs no value at all.

Example

vec_resize.rs in full — pasted here by tools/run_examples.py from the file CI compiles and runs.

fn main() {
    // Grow: the new slots are clones of the value you pass.
    let mut v = vec![1, 2];
    v.resize(5, 0);
    println!("{v:?}");

    // Shrink: the value is ignored and the tail is dropped.
    let mut v = vec![1, 2, 3, 4, 5];
    v.resize(2, 99);
    println!("{v:?}");

    // Same length: nothing happens at all.
    let mut v = vec![1, 2, 3];
    v.resize(3, 0);
    println!("{v:?}");

    // T: Clone, and it really clones — one value in, n copies out.
    let mut v: Vec<String> = Vec::new();
    v.resize(3, String::from("x"));
    v[0].push('!');
    println!("{v:?}");

    // Which is why a non-Clone default needs resize_with instead.
    let mut v: Vec<Vec<u8>> = vec![vec![1]];
    v.resize_with(3, Vec::new);
    println!("resize_with for a fresh value each time: {v:?}");

    // Padding a buffer to a fixed width.
    let mut record = b"abc".to_vec();
    record.resize(8, b'.');
    println!("{}", String::from_utf8(record).unwrap());

    // The tail really is dropped when shrinking.
    struct Noisy(u8);
    impl Drop for Noisy {
        fn drop(&mut self) { println!("  dropping {}", self.0); }
    }
    {
        let mut v = vec![Noisy(1), Noisy(2), Noisy(3)];
        println!("resize(1, ..):");
        v.resize_with(1, || Noisy(0));
        println!("  survivor {}", v[0].0);
    }
}

Verified output of vec_resize.rs — regenerated by tools/run_examples.py, never hand-typed.

[1, 2, 0, 0, 0]
[1, 2]
[1, 2, 3]
["x!", "x", "x"]
resize_with for a fresh value each time: [[1], [], []]
abc.....
resize(1, ..):
  dropping 2
  dropping 3
  survivor 1
  dropping 1

See also

Vec::resize in the standard library ↗