vec![1, 2, 3]
Collections & Iterators

Meaning

The vec! macro creates a new Vec (heap-allocated growable array) with the given elements. It is the idiomatic way to initialize a vector with known values at compile time.

Primary Function

Data structure initialization

Communicative Purpose

Concisely create a vector populated with initial elements without manual push calls.

Pattern

vec![;]

Core Structure

vec![;]

Função primária

Data structure initialization

Propósito comunicativo

Concisely create a vector populated with initial elements without manual push calls.

Situações de gatilho

Need a vector with predefined test data, small fixed collections, or when collecting a known set of values into a Vec.

Contextos

Rust standard library, any Rust codebase using collections, CLI tools, web services, embedded Rust with alloc.

Padrão

vec![;]

Estrutura central

vec![;]

Slots de substituição

Comma-separated element expressions of the same type (or types coercible to a common type). Each element is evaluated and moved/cloned into the vector.

Colocados típicos

  • .push()
  • .pop()
  • .iter()
  • .len()
  • for loops
  • pattern matching
  • .extend()
  • .drain()

Substituições comuns

  • Vec::new() for empty vector
  • vec![value
  • n] for n clones of value
  • Vec::from(array) for array conversion
  • collect() on iterators.

Erros comuns

Using vec![x; n] with non-Clone types (requires Clone); confusing vec![] with array syntax [x; n] (fixed-size stack allocation); expecting vec! to work in const contexts without const_evaluatable_checked.

Similar / contraste

Array literal [1, 2, 3] — fixed size, stack-allocated, no heap allocation. Vec::new() — empty vector, no initial data. vec![value; n] — repeated value form, requires Clone.

Interferências

Coming from C++: vec! is a macro, not a constructor; it allocates on the heap like std::vector but syntax differs. Coming from Python: [1,2,3] is a list (similar to Vec), but vec! is explicit about heap allocation and type homogeneity.

Família do chunk

  • Vec creation
  • Collection literals
  • Macro usage
  • Heap allocation patterns

Nuance

vec![x; n] clones x n times, so x must implement Clone. For non-Clone types, list each element explicitly. The macro expands to heap allocation; in no_std contexts, requires alloc crate. Elements are moved into the vector unless they are Copy.

Efeito pragmático

Eliminates boilerplate push loops; makes intent explicit; ensures correct capacity from start.

Dica de memória

vec! = vector bang = heap vector with initial elements

Nota

The vec! macro is the canonical way to create vectors with initial data in Rust. It guarantees the vector has exactly the specified capacity and length.

Upgrade path

vec![value; n] for repeated values; Vec::with_capacity(n) for pre-allocation without initial data; Box::new([...]) for boxed slice when size is truly fixed.

Frequência: Very highFormulaicidade: Semi-fixedPrioridade de aquisição: Automatic productionPrioridade de output: BothTag de espaçamento: Immediate

Log in to save chunks.