std-method
Vec::resize
Grow by cloning the supplied fill value as needed or shrink by dropping trailing elements. The final length is new_len. Use resize_with to generate distinct values. Expected stdout (not executed): [4, 5, 0, 0] [4] Common mistake: Assuming cloning an Rc fill value creates independent allocations.
Formal Syntax & Structure
rust syntaxrust
pub fn resize(&mut self, new_len: usize, value: T) where T: CloneWorking Example
Example Snippetrust
fn main() { let mut counts = vec![4, 5]; counts.resize(4, 0); println!("{counts:?}"); counts.resize(1, 0); println!("{counts:?}");}