Introduction
A struct (short for 'structure') is a custom data type that lets you package together multiple related values under one name, each with its own field name and type. Structs are the foundation for modeling real-world entities in Rust, similar to classes without inheritance or objects/records in other languages. Rust supports three flavors: named-field structs, tuple structs, and unit structs.
Cricket analogy: A struct is like a player's official scorecard entry that bundles name, runs, balls faced, and strike rate under one record; Rust's three flavors mirror a full batting card, a simple pair like (runs, balls), or just a placeholder 'did not bat' marker.
Syntax
// Named-field struct
struct Point {
x: i32,
y: i32,
}
// Tuple struct
struct Color(i32, i32, i32);
// Unit struct
struct Marker;Explanation
A named-field struct like Point declares each field with an explicit name and type, and you create an instance with Point { x: 1, y: 2 }. If a variable already has the same name as a field, you can use field init shorthand, e.g. Point { x, y }. Tuple structs like Color group values positionally without field names, accessed with .0, .1, .2. Unit structs like Marker have no fields at all and are useful when you only care about a type's identity, for example when implementing a trait with no data. You can also use struct update syntax, Point { x: 5, ..other }, to build a new instance that copies the remaining fields from an existing one.
Cricket analogy: Point { x: 1, y: 2 } is like filling out a named scorecard with explicit labels for runs and balls; field init shorthand is like writing just 'runs, balls' when local variables already carry those names, and struct update syntax reuses most of a template card while changing one stat.
Example
struct Point {
x: i32,
y: i32,
}
fn main() {
let p1 = Point { x: 1, y: 2 };
let p2 = Point { x: 10, ..p1 };
println!("p1 = ({}, {})", p1.x, p1.y);
println!("p2 = ({}, {})", p2.x, p2.y);
let black = Color(0, 0, 0);
println!("black.0 = {}", black.0);
}
struct Color(i32, i32, i32);
// Output:
// p1 = (1, 2)
// p2 = (10, 2)
// black.0 = 0Key Takeaways
- Named-field structs group values with explicit field names; access with dot notation.
- Tuple structs group values positionally; access with
.0,.1, etc. - Unit structs carry no data and are mainly useful for trait implementations.
- Field init shorthand lets you write
Point { x, y }instead ofPoint { x: x, y: y }. - Struct update syntax
..othercopies remaining fields from an existing instance.
Practice what you learned
1. Which struct definition is a tuple struct?
2. How do you access the first field of a tuple struct instance `c`?
3. What does struct update syntax `Point { x: 5, ..other }` do?
4. What is a unit struct primarily useful for?
5. What is field init shorthand?
Was this page helpful?
You May Also Like
Enums in Rust
Enums define a type by enumerating its possible variants, each of which can optionally hold its own data.
Methods and impl Blocks in Rust
Learn how to attach behavior to structs and enums using impl blocks, self receivers, and associated functions.
Tuples and Arrays in Rust
Tuples group a fixed set of differently-typed values together, while arrays hold a fixed-size list of same-typed values.
Traits in Rust
Traits define shared behavior that types can implement, similar to interfaces in other languages.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics