← All notes

Rust string slices and array slices

Answer: A slice is a kind of reference without ownership. Write string slices as &str. String literals stay immutable because &str is an immutable reference.

Slices

You can reference a contiguous sequence of collection elements with a slice instead of the whole collection. A string slice refers to part of a String.

Array slices

Array slices store a first-element reference plus a length, matching string slices. The slice type is a dynamically-sized view into a contiguous sequence.

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 and notices: The Rust Programming Language book (rust-lang/book): Copyright (c) 2010 The Rust Project Developers; MIT OR Apache-2.0, used under MIT (https://github.com/rust-lang/book/blob/main/LICENSE-MIT). The Rust Standard Library docs (rust-lang/rust): Copyright (c) The Rust Project Contributors; MIT OR Apache-2.0, used under MIT (https://github.com/rust-lang/rust/blob/master/LICENSE-MIT). Text was condensed and changed from the originals.

License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted). Sources: The Rust Project (The Rust Programming Language book), The Slice Type - The Rust Programming Language, MIT OR Apache-2.0; The Rust Standard Library, slice primitive type, MIT OR Apache-2.0. Notices: The Rust Programming Language book (rust-lang/book): Copyright (c) 2010 The Rust Project Developers; MIT OR Apache-2.0, used under MIT (https://github.com/rust-lang/book/blob/main/LICENSE-MIT). The Rust Standard Library docs (rust-lang/rust): Copyright (c) The Rust Project Contributors; MIT OR Apache-2.0, used under MIT (https://github.com/rust-lang/rust/blob/master/LICENSE-MIT).

Cite this

Permanent ID: iil:note:t6cfz6c6

iinventedlight. 2026. "Rust string slices and array slices." iinventedlight Library, iil:note:t6cfz6c6, version of 2026-11-09. https://iinventedlight.com/library/notes/rust-slices/. License: CC-BY-4.0.

@misc{iil_note_t6cfz6c6,
  author = {{iinventedlight}},
  title = {{Rust string slices and array slices}},
  howpublished = {iinventedlight Library},
  year = {2026},
  date = {2026-11-09},
  url = {https://iinventedlight.com/library/notes/rust-slices/},
  note = {Permanent ID iil:note:t6cfz6c6. License: CC-BY-4.0.},
}

Markdown

Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com.

For language models and agents: start at /llms.txt | /agents.md | /.well-known/agent-library.json. This page in Markdown: /library/notes/rust-slices.md.