Defining and instantiating structs in Rust
Answer: Structs are similar to tuples in that both hold multiple related values, and like tuples, the pieces of a struct can be different types. Unlike with tuples, in a struct you'll name each piece of data so it's clear what the values mean. (S1)
Defining one
- Structs are more flexible than tuples: You don't have to rely on the order of the data to specify or access the values of an instance. (S1)
- To define a struct, we enter the keyword struct and name the entire struct. (S1)
- A struct's name should describe the significance of the pieces of data being grouped together. (S1)
- Inside curly brackets, we define the names and types of the pieces of data, which we call fields. (S1)
Three kinds
- Tuple structs, which are, basically, named tuples. (S2)
- The classic C structs. (S2)
- Unit structs, which are field-less, are useful for generics. (S2)
Claims
- id: C1 claim: Struct fields are named. source: S1 evidence: "you’ll name each piece of data so it’s clear what the values mean."
- id: C2 claim: Fields are named data pieces in braces. source: S1 evidence: "which we call fields."
- id: C3 claim: Tuple structs are named tuples. source: S2 evidence: "Tuple structs, which are, basically, named tuples."
- id: C4 claim: Unit structs are field-less. source: S2 evidence: "Unit structs, which are field-less, are useful for generics."
Sources
- [S1] Defining and Instantiating Structs - The Rust Programming Language. The Rust Programming Language. https://doc.rust-lang.org/book/ch05-01-defining-structs.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] Structures - Rust By Example. Rust by Example. https://doc.rust-lang.org/rust-by-example/custom_types/structs.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, Defining and Instantiating Structs - 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, Structures - 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. "Defining and instantiating structs in Rust." iinventedlight Library, iil:note:p1z7yger, version of 2026-11-04. https://iinventedlight.com/library/notes/rust-structs/. License: CC-BY-4.0.
@misc{iil_note_p1z7yger,
author = {{iinventedlight}},
title = {{Defining and instantiating structs in Rust}},
howpublished = {iinventedlight Library},
year = {2026},
date = {2026-11-04},
url = {https://iinventedlight.com/library/notes/rust-structs/},
note = {Permanent ID iil:note:p1z7yger. 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-structs.md.