2026-04-07
Structs & Enums: The "Blueprint" and "State Machine"
In JavaScript, you often use plain objects for everything. In Rust, we use Structs and Enums to model data with absolute precision. Think of it as the...
In JavaScript, you often use plain objects for everything. In Rust, we use Structs and Enums to model data with absolute precision. Think of it as the difference between a "Sketch" and an "Engineering Blueprint."
🏛️ 1. Structs: The "Blueprint" Contract
A Struct is the contract for the shape of your data. It is a strictly typed object that ensures every instance has the exact same fields and types.
TIP: 🏗️ Build Faster with "Struct Updates"
Similar to the JS spread operator (
...), Rust has the "Struct Update Syntax" for creating new versions of your data:let user2 = User ;
🧠 2. The Implementation (impl): Separation of Logic
In JS classes, you put data and functions in the same box. In Rust, we separate the Data (Struct) from the Logic (Implementation).
🥊 3. Enums: The "State Machine" Contract
While a Struct is about "AND" (I have a width AND a height), an Enum is about "OR" (I am EITHER a Success OR an Error).
In TypeScript, you'db use a "Discriminated Union":
type Message =
| { type: 'quit' }
| { type: 'move', x: number, y: number }
| { type: 'write', text: string };
In Rust, Enums are much more powerful because they carry data directly and are checked by the compiler for exhaustiveness.
IMPORTANT: The Magic: Pattern Matching
You don't need a
typeorkindfield. Usematchto extract the data safely! The compiler will literally refuse to run your code if you forget to handle one of the states (e.g., forgetting theErrorstate in an API call).
💡 What these Unlock
- Exhaustive Logic: Using Enums ensures your application handles every possible state (like
Loading,Success,Error) without "Undocumented States" causing bugs. - Data Integrity: Structs ensure that your data remains in a "Known-Good" state throughout the entire application lifecycle.