← All notes

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

Sources

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

Permanent ID: iil:note:4twgm3jh

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.},
}

Markdown

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.

All artifact files (JSON index)

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.