Skip to content
Guest
Rust · Course Syllabus Show lessons
Types & Patterns · 9 min read · Intermediate

Structs, impl Blocks & Associated Functions

Model domain data with structs, attach methods with impl, and write constructors like Self::new().

What you'll learn

  • Define named structs and derive Debug / Clone
  • Choose between &self, &mut self, and self receivers
  • Write clean constructors with associated functions

Overview

Model domain data with structs, attach methods with impl, and write constructors like Self::new().

Code Example

#[derive(Debug)]
struct Course {
    slug: String,
    hours: u32,
}

impl Course {
    fn summary(&self) -> String {
        format!("{} ({}h)", self.slug, self.hours)
    }
}

Key Takeaways

  • Define named structs and derive Debug / Clone
  • Choose between &self, &mut self, and self receivers
  • Write clean constructors with associated functions

Quick Check

Check Your Understanding

Score: 0 / 1

1. What does `#[derive(Debug)]` on a Rust struct automatically generate?

Study Workspace

Finished this lesson?

Your progress is saved locally in your browser, no account needed.

Search courses, lessons, and references

Type to search across all courses, lessons, and quick references.

Press Esc to close Powered by Pagefind