On this page
  1. Vec
  2. Availability
  3. Syntax & Overview
  4. Notes & Pitfalls
  5. Example

Vec

Prev: Result | Up: builtins | Next: String
Rel: builtins, Ownership, Borrowing & Lifetimes
Ref: Rust Docs — Vec
Tags: #public #prog #rust #builtins #tier/core


Availability

Built-in / Prelude (Prelude Type) — available with zero use imports.

Syntax & Overview

Vec<T>  /* macro: vec![1, 2, 3] */

Contiguous, growable heap-allocated array (ptr, len, cap) with amortized $O(1)$ push.

Notes & Pitfalls

Example

fn main() {
    let mut nums = Vec::with_capacity(4);
    nums.extend([10, 20, 30]);
    nums.push(40);
    assert_eq!(nums.pop(), Some(40));
    assert_eq!(nums.as_slice(), &[10, 20, 30]);
}

Linked from

Related notes