Chapter 28: Rust Structs

Rust structs — the real workhorse for creating your own custom data types.

In Rust, when you want to group related data together with names, with meaning, and usually with behavior (methods), you use structs. This is where your program starts to feel like real software engineering — not just playing with numbers and strings.

Let me explain Rust structs like your patient teacher sitting next to you with VS Code open: slowly, clearly, with many small-to-medium examples you can copy-paste and run right now, Hyderabad-flavored analogies, comparisons to other languages, and building from very simple → medium → advanced patterns.

1. What is a Struct? (The Core Idea)

A struct (short for structure) lets you create a named collection of fields — each field has a name and a type.

  • Fields have meaningful names (unlike tuples which only have positions .0, .1)
  • You can add methods to structs (via impl)
  • Rust has three kinds of structs (you’ll use the first one 95% of the time):
Kind Syntax When to use Fields named? Tuple-like?
Classic / Named struct Person { name: String, … } Most common — normal data with names Yes No
Tuple struct struct Point(i32, i32); When order matters, no need for names No Yes
Unit struct struct Empty; Marker / zero-sized type (rare) No fields

We’ll focus mostly on classic named structs — that’s what you’ll write every day.

2. Defining & Creating a Classic Struct

Rust
  • Fields are public by default (no pub needed inside same module)
  • Order of fields in initialization does not matter — Rust matches by name
  • Field init shorthand: if variable name == field name → just write the name

3. Accessing & Mutating Fields

Rust
  • .field_name syntax (dot access)
  • Struct must be mut to change any field

4. Adding Methods with impl (This is Where Structs Become Powerful)

Rust
  • &self = immutable borrow (read-only method)
  • &mut self = mutable borrow (changes the struct)
  • self = takes ownership (consumes the struct — rare)
  • No self = associated function (like static method — call on Type::name())

5. Struct Update Syntax (Very Convenient)

Rust
  • ..base copies remaining fields from base
  • Very useful for builder-like patterns or small modifications

6. Tuple Structs (When Names Aren’t Needed)

Rust

Use when the meaning is obvious from order (colors, coordinates, vectors).

7. Unit Structs (Rare but Useful)

Rust

Common in traits/impl patterns or zero-sized types.

Teacher Summary Table

Feature Classic struct Tuple struct Unit struct
Syntax struct Name { field: Type } struct Name(Type, Type); struct Name;
Fields named? Yes No (positions only) No fields
Methods possible? Yes Yes Yes
Typical use Normal data records Simple wrappers (Color, Point) Markers, flags
Readability Excellent (named fields) OK for small fixed tuples

Practice Challenge for Today

Create cargo new rust_structs and try:

  1. struct User { username: String, email: String, active: bool, sign_in_count: u64 }
  2. Add method increment_sign_in(&mut self)
  3. Add associated function new(username: String, email: String) -> User
  4. Use struct update syntax to create a deactivated copy
  5. Create tuple struct Coordinates(f64, f64) + method distance_from_origin(&self)

Want to go deeper next?

  • Methods vs associated functions in detail?
  • Structs + ownership/borrowing (very important with String fields)?
  • Enums (the other big custom type — often used with structs)?
  • Or impl blocks, traits on structs?

Just tell me — your Rust teacher from Hyderabad is right here! 🦀🚀

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *