100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Rust

Structs in Rust

Structs let you group related values into a single named type with fields you access by name.

Data StructuresBeginner8 min readJul 8, 2026
Analogies

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

rust
// 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

rust
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 = 0

Key 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 of Point { x: x, y: y }.
  • Struct update syntax ..other copies remaining fields from an existing instance.

Practice what you learned

Was this page helpful?

Topics covered

#Rust#RustProgrammingStudyNotes#Programming#StructsInRust#Structs#Syntax#Explanation#Example#StudyNotes#SkillVeris