Structs and Enums
Structs group related fields. Enums represent sum types — a value is one of several variants. Both support methods via impl blocks.
Structs
struct User {
name: String,
age: u32,
active: bool,
}
let user = User {
name: String::from("Alice"),
age: 30,
active: true,
};
Tuple Structs
struct Color(u8, u8, u8);
let black = Color(0, 0, 0);
Enums
enum Message {
Quit,
Move { x: i32, y: i32 },
Write(String),
ChangeColor(u8, u8, u8),
}
Methods
impl User {
fn new(name: &str) -> Self {
User { name: name.to_string(), age: 0, active: true }
}
fn greet(&self) { println!("Hi, {}!", self.name); }
}
Option Enum
enum Option<T> { Some(T), None }
let x: Option<i32> = Some(5);
let y: Option<i32> = None;
Common Pitfalls
- Ignoring returned errors in Go or fighting the borrow checker in Rust.
- Premature optimization before profiling.
- Skipping tests for concurrent and error paths.
- Not reading standard library documentation.
Best Practices
- Follow language idioms (Go: explicit errors; Rust: ownership).
- Write table-driven tests.
- Use
go fmt/cargo fmtconsistently. - Keep functions small and focused.
Memory and Performance Notes
Enums with data carry same cost as structs — tagged union layout.
Exercise
Define an enum Shape with Circle and Rectangle variants. Implement area() method for each.
Hint: Use match for exhaustive enum handling.
Summary
Practice these concepts in small programs before building production services.
Debugging Checklist
- Read the full error message.
- Reduce to minimal reproduction.
- Check types and return values.
- Add logging at decision points.
- Write a test that catches the bug.
Real-World Application
Production services combine these fundamentals with logging, metrics, and graceful degradation.
Further Reading
Official language documentation, effective guides, and mature open-source projects.
Additional Examples
Consider how this topic applies in a larger project:
// Break the problem into smaller functions
// Test each function independently
// Integrate incrementally
Working through variations of the examples above builds deeper understanding than reading alone.
Interview and Review Questions
- Explain the core concept of this topic in your own words.
- What happens when this code runs with edge-case input (empty, null, zero, max value)?
- How would you debug a bug related to this topic in production?
- What are the performance implications of the approach shown here?
- How does this feature compare to the equivalent in another language you know?
Related Topics in This Path
Review adjacent pages in the learning path before and after this one. Concepts build on each other — skipping ahead often leads to confusion when later pages assume mastery of earlier material.
Return to the section index if you need to fill gaps in prerequisite knowledge.
Tooling Tips
- Enable all compiler or analyzer warnings during development.
- Use version control with small, focused commits for each exercise.
- Pair reading with typing — reproduce every code example by hand.
- Run tests or compile after every change to catch errors early.
- Keep a personal notes file linking concepts to your own project experiences.