Meaning
Creates an empty vector (growable array) with zero allocation. The type parameter is inferred from context or must be explicitly annotated. Use when starting a collection that will be built incrementally.
Primary Function
Data structures
Communicative Purpose
Initialize an empty growable collection for subsequent element insertion
Pattern
Vec::new()
Core Structure
Vec::new()
Função primária
Data structures
Propósito comunicativo
Initialize an empty growable collection for subsequent element insertion
Situações de gatilho
Building a list incrementally in a loop, returning an empty collection from a function, starting an accumulator for fold/reduce operations
Contextos
Rust standard library, systems programming, CLI tools, web services, embedded Rust
Padrão
Vec::new()
Estrutura central
Vec::new()
Slots de substituição
none (fully fixed)
Colocados típicos
- vec.push()
- vec.extend()
- vec.iter()
- for loops
- collect()
Substituições comuns
- vec![] macro (more idiomatic for inline)
- Vec::with_capacity(n) when approximate size known
Erros comuns
Forgetting type annotation when inference fails (e.g., let v = Vec::new(); without later use), using Vec::new() when capacity is predictable (wastes reallocations)
Similar / contraste
vec![] macro — identical runtime behavior, preferred for inline initialization; Vec::with_capacity(n) — pre-allocates memory, use when size estimate exists
Interferências
Coming from C++: std::vector() may allocate; Vec::new() is guaranteed zero-allocation until first push. Coming from Python: [] creates list immediately; Vec::new() defers allocation entirely.
Família do chunk
- vec![]
- Vec::with_capacity
- Vec::from
- Vec::into_iter
- Vec::push
Nuance
Zero-cost until first element inserted. Type must be inferrable from subsequent usage or explicitly annotated (e.g., Vec::<i32>::new()). Prefer vec![] for readability when initializing inline.
Efeito pragmático
Zero-cost abstraction for empty vector creation; enables type-safe incremental building
Dica de memória
Empty vec, zero alloc, push to grow
Upgrade path
Vec::with_capacity(n) when approximate final size is known
Log in to save chunks.