macro

vec! — Create a vector from elements or repeated values

Create a Vec from a list or a repeated element. The repetition form uses cloning when required; it does not repeatedly evaluate the element expression for each slot. Cloning a shared pointer may still refer to the same underlying object. Expression result: Vec<T>. Expected result (not executed): stdout: '[3, 5, 8, 13]; [0, 0]\n'; no stderr. Common mistake: Assuming vec![Rc::new(data); 3] allocates three independent data objects.

Formal Syntax & Structure

rust syntaxrust
vec![a, b, c] or vec![value; count]

Working Example

Example Snippetrust
fn main() {    let mut stock = vec![3, 5, 8];    stock.push(13);    let empty_slots = vec![0_u32; 2];    println!("{stock:?}; {empty_slots:?}");}
Rust Reference