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).
License: CC-BY-4.0 · 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).
Cite this
iinventedlight. 2026. "Rust vectors: Vec<T>, indexing versus get, and borrowing." iinventedlight Library, iil:note:msgvp1yr, version of 2026-11-04. https://iinventedlight.com/library/notes/rust-vectors/. License: CC-BY-4.0.
@misc{iil_note_msgvp1yr,
author = {{iinventedlight}},
title = {{Rust vectors: Vec<T>, indexing versus get, and borrowing}},
howpublished = {iinventedlight Library},
year = {2026},
date = {2026-11-04},
url = {https://iinventedlight.com/library/notes/rust-vectors/},
note = {Permanent ID iil:note:msgvp1yr. License: CC-BY-4.0.},
}
Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com.
For language models and agents: start at /llms.txt. This page in Markdown: /library/notes/rust-vectors.md.