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
- id: C1 claim: A slice is a kind of reference and does not have ownership. source: S1 evidence: "A slice is a kind of reference, so it does not have ownership."
- id: C2 claim: The type that signifies a string slice is written as &str. source: S1 evidence: "The type that signifies \"string slice\" is written as &str: <Listing file-name=\"src/main.rs\"> rust </Listing> We"
- id: C3 claim: String literals are immutable because &str is an immutable reference. source: S1 evidence: "This is also why string literals are immutable; &str is an immutable reference."
- id: C4 claim: A slice is a dynamically-sized view into a contiguous sequence. source: S2 evidence: "A dynamically-sized view into a contiguous sequence, [T] ."
Sources
- [S1] The Slice Type - The Rust Programming Language. The Rust Project (The Rust Programming Language book). https://doc.rust-lang.org/book/ch04-03-slices.html . License: MIT OR Apache-2.0. Accessed 2026-11-09.
- [S2] slice primitive type. The Rust Standard Library. https://doc.rust-lang.org/std/primitive.slice.html . License: MIT OR Apache-2.0. Accessed 2026-11-09.
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
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.},
}
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.