Code Example Template
Deep-Dive System Documentation
Structs: Custom Data Groupings
Structs are the primary compound types in Rust used to model domain objects. Rust supports three distinct struct layouts:
1. Named-Field Structs
The standard struct form where each field has an explicit name and type. Useful for complex models.
struct KeyCoordinates {
x: i32,
y: i32,
}
let pt = KeyCoordinates { x: 100, y: 200 };
2. Tuple Structs
Fields do not have names; they are accessed by 0-based indices. Ideal for lightweight type wrappers.
struct Keystroke(u32, bool); // key_code, is_down
let event = Keystroke(0x41, true);
let key = event.0;
3. Unit Structs
Fields are omitted entirely. Used to implement traits on marker types without storing data.
struct GlobalListener;
Performance Metrics
- Storage Cost: Packed contiguously in memory on the stack.
- Access speed: Compile-time offsets mean zero-runtime field access costs.
Quick Reference Guide
struct Point { x: f64, y: f64 }
let p = Point { x: 1.0, y: 2.0 };