---
source: https://iinventedlight.com/library/notes/rust-vectors.md
title: "Rust vectors: Vec<T>, indexing versus get, and borrowing"
license: CC-BY-4.0
license_url: https://creativecommons.org/licenses/by/4.0/
source_license: "MIT or Apache-2.0 (rust-lang/book; Copyright (c) 2010 The Rust Project Developers; https://github.com/rust-lang/book/blob/main/LICENSE-MIT); MIT or Apache-2.0 (rust-lang/rust-by-example; Copyright (c) 2014 Jorge Aparicio; https://github.com/rust-lang/rust-by-example/blob/master/LICENSE-MIT); prose restates or closely paraphrases cited source lines"
license_family: rust-mit
rule_check: "needs rule check (family not in auto-publish rule; check.mjs auto-approves only usgov and arxiv-ccby)"
attribution: "Summary by iinventedlight (AI-assisted). Sources: The Rust Programming Language, Storing Lists of Values with Vectors - The Rust Programming Language, MIT or Apache-2.0 (rust-lang/book; Copyright (c) 2010 The Rust Project Developers; https://github.com/rust-lang/book/blob/main/LICENSE-MIT); Rust by Example, Vectors - Rust By Example, MIT or Apache-2.0 (rust-lang/rust-by-example; Copyright (c) 2014 Jorge Aparicio; https://github.com/rust-lang/rust-by-example/blob/master/LICENSE-MIT)."
author: "iinventedlight (synthesis of cited sources); text drafted with AI assistance"
date: 2026-11-04
status: published
checked_on: 2026-11-04
as_of: 2026-11-04
confidence: medium
untested: "No code was run for this note; only the fetched documentation pages were read."
category: coding-references
topic_area: "code-ai-eng: Rust"
tags: ['rust', 'vec', 'collections']
sources:
  - id: S1
    title: "Storing Lists of Values with Vectors - The Rust Programming Language"
    publisher: The Rust Programming Language
    url: https://doc.rust-lang.org/book/ch08-01-vectors.html
    accessed: 2026-11-04
    license: "MIT or Apache-2.0 (rust-lang/book; Copyright (c) 2010 The Rust Project Developers; https://github.com/rust-lang/book/blob/main/LICENSE-MIT)"
  - id: S2
    title: "Vectors - Rust By Example"
    publisher: Rust by Example
    url: https://doc.rust-lang.org/rust-by-example/std/vec.html
    accessed: 2026-11-04
    license: "MIT or Apache-2.0 (rust-lang/rust-by-example; Copyright (c) 2014 Jorge Aparicio; https://github.com/rust-lang/rust-by-example/blob/master/LICENSE-MIT)"
---
# Rust vectors: Vec<T>, indexing versus get, and borrowing

**Answer:** Vec<T>, also known as a vector, lets you store more than one value in a single data structure that puts all the values next to each other in memory. Vectors can only store values of the same type. (S1)

## Creating one

- Rust conveniently provides the vec! macro, which will create a new vector that holds the values you give it. (S1)
- More often, you'll create a Vec<T> with initial values, and Rust will infer the type of value you want to store. (S1)
- Like slices, their size is not known at compile time, but they can grow or shrink at any time. (S2)
- The capacity indicates how much memory is reserved for the vector; when that threshold needs to be surpassed, the vector is reallocated with a larger capacity. (S2)

## Reading elements

- There are two ways to reference a value stored in a vector: via indexing or by using the get method. (S1)
- When we use the get method with the index passed as an argument, we get an Option<&T> that we can use with match. (S1)
- Indexing with [] will cause the program to panic when it references a nonexistent element. (S1)

## Borrowing rules still apply

- Recall the rule that states you can't have mutable and immutable references in the same scope. (S1)
- That rule applies when we hold an immutable reference to the first element in a vector and try to add an element to the end. (S1)
- Iterating over a vector, whether immutably or mutably, is safe because of the borrow checker's rules. (S1)

## Claims
- id: C1
  claim: Vectors store values of one type next to each other in memory.
  source: S1
  evidence: "Vectors can only store values that are of the same type."
- id: C2
  claim: get returns an Option<&T>.
  source: S1
  evidence: "we get an Option<&T> that we can"
- id: C3
  claim: A vector is reallocated when it outgrows its capacity.
  source: S2
  evidence: "the vector is reallocated with a larger capacity."
- id: C4
  claim: Vectors can grow or shrink at any time.
  source: S2
  evidence: "time, but they can grow or shrink at any time."

## Sources
- [S1] Storing Lists of Values with Vectors - The Rust Programming Language. The Rust Programming Language. https://doc.rust-lang.org/book/ch08-01-vectors.html . License: MIT or Apache-2.0 (rust-lang/book; Copyright (c) 2010 The Rust Project Developers; https://github.com/rust-lang/book/blob/main/LICENSE-MIT). Accessed 2026-11-04.
- [S2] Vectors - Rust By Example. Rust by Example. https://doc.rust-lang.org/rust-by-example/std/vec.html . License: MIT or Apache-2.0 (rust-lang/rust-by-example; Copyright (c) 2014 Jorge Aparicio; https://github.com/rust-lang/rust-by-example/blob/master/LICENSE-MIT). Accessed 2026-11-04.

License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted).
