Rust arrays: fixed-length collections of one type
Answer: Stored in contiguous memory, an array collects objects of the same type T. Compile-time length is part of the type, written [T; length]. In Rust that length is fixed.
Type signature and memory
Values of one type T sit in contiguous memory when stored in an array.
Write an array with brackets []. The type signature [T; length] carries the compile-time length.
A slice looks similar, yet its length is not known at compile time.
The slice value is a two-word object: a pointer to the data, then the length.
Fixed length, stack, and access
Tuples and arrays are Rust's two primitive compound types.
Every element of an array must have the same type, which is unlike a tuple.
The length is fixed, which is unlike arrays in some other languages.
An array isn't as flexible as the vector type. If you're unsure which type fits, chances are you should use a vector.
Choose an array when the number of elements will not need to change.
After the semicolon, the example uses 5 to show five elements.
An array is one chunk of memory with a known, fixed size. It can be allocated on the stack.
Index access is allowed. An index past the end of the array is invalid.
See also
Claims
- id: C1 claim: Every element of an array must have the same type. source: S2 evidence: "every element of an array must have the same type"
- id: C2 claim: Arrays in Rust have a fixed length. source: S2 evidence: "arrays in Rust have a fixed length"
- id: C3 claim: Length is part of the type signature [T; length]. source: S1 evidence: "type signature [T; length]"
- id: C4 claim: An array can be allocated on the stack. source: S2 evidence: "can be allocated on the stack"
- id: C5 claim: A slice's length is not known at compile time. source: S1 evidence: "their length is not known at compile time"
Sources
- [S1] Arrays and Slices. Rust by Example. https://doc.rust-lang.org/rust-by-example/primitives/array.html . License: MIT OR Apache-2.0. Accessed 2026-11-23.
- [S2] Data Types. The Rust Programming Language. https://doc.rust-lang.org/book/ch03-02-data-types.html . License: MIT OR Apache-2.0. Accessed 2026-11-23.
License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: Rust by Example, Arrays and Slices; The Rust Programming Language, Data Types. MIT OR Apache-2.0 (https://raw.githubusercontent.com/rust-lang/rust-by-example/master/LICENSE-MIT). Copyright the Rust Project Developers. MIT OR Apache-2.0 (https://raw.githubusercontent.com/rust-lang/book/main/LICENSE-MIT). Copyright the Rust Project Developers. Text was condensed and changed from the originals.
License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted). Sources: Rust by Example, Arrays and Slices; The Rust Programming Language, Data Types. MIT OR Apache-2.0 (https://raw.githubusercontent.com/rust-lang/rust-by-example/master/LICENSE-MIT). Copyright the Rust Project Developers. MIT OR Apache-2.0 (https://raw.githubusercontent.com/rust-lang/book/main/LICENSE-MIT). Copyright the Rust Project Developers.
Cite this
iinventedlight. 2026. "Rust arrays: fixed-length collections of one type." iinventedlight Library, iil:note:4twgm3jh, version of 2026-11-23. https://iinventedlight.com/library/notes/rust-arrays/. License: CC-BY-4.0.
@misc{iil_note_4twgm3jh,
author = {{iinventedlight}},
title = {{Rust arrays: fixed-length collections of one type}},
howpublished = {iinventedlight Library},
year = {2026},
date = {2026-11-23},
url = {https://iinventedlight.com/library/notes/rust-arrays/},
note = {Permanent ID iil:note:4twgm3jh. License: CC-BY-4.0.},
}
Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com. Or Report a correction (no account needed).
Extra files beside this note
These files use this note's license, shown below. Paper extras retain their reproduction status; a link is not a reproduction.
- rust-arrays.schema.json (schema; CC-BY-4.0)
- rust-arrays.table.json (table; CC-BY-4.0)
For language models and agents: start at /llms.txt | /agents.md | /.well-known/agent-library.json. This page in Markdown: /library/notes/rust-arrays.md.