Overview
5 min read
In TypeScript you model data with object literals, interfaces, type aliases, and union types. Rust gives you a tighter, compiler-enforced toolkit: structs for fixed records, enums for “one of several shapes,” Option<T> instead of null/undefined, pattern matching to destructure and branch in one step, and impl blocks to attach behavior to data. This section maps each TypeScript habit onto its idiomatic Rust counterpart so you can model a domain the Rust way — nominally typed, exhaustively checked, and with no hidden undefined.
What You’ll Learn
Section titled “What You’ll Learn”- How a TypeScript
interface+ object literal becomes a singlestructthat exists at compile time and runtime, and what it means for a struct to own its fields - The leaner struct shapes — tuple structs and unit structs — and the newtype pattern that gives a primitive a distinct, type-checked identity
- How TypeScript union and discriminated-union types map to enums with data-carrying variants, with mandatory exhaustiveness instead of an opt-in
nevertrick - Why Rust has no
null/undefined, and howOption<T>withSome/None, the combinators (map,and_then,unwrap_or), and the?operator replace?.and?? - How pattern matching fuses destructuring, narrowing, and branching into one exhaustive
match(plusif let,let ... else, andwhile let) - How class methods become
implblocks, and how&self/&mut self/selfmake a method’s borrowing rules part of its signature - How
staticmethods become associated functions, including the conventionalSelf::newconstructor and a first look at the builder pattern - A light introduction to associated types and associated constants (full treatment arrives in Section 09)
- How object property shorthand becomes field init shorthand, and how
..otherstruct update syntax mimics the spread operator
Topics
Section titled “Topics”| Topic | Description |
|---|---|
| Structs | TypeScript objects/interfaces → structs: field definitions, instantiation, ownership of fields, and #[derive(Debug, Clone)]. |
| Tuple Structs and Unit Structs | Positional tuple structs and fieldless unit structs, with a teaser for the newtype pattern. |
| Enums and Data-Carrying Variants | TypeScript union types → enums: data-carrying variants, and how enums compare to discriminated unions. |
| The Option Type | null/undefined → Option<T>: Some/None, the map/and_then/unwrap_or combinators, and ? with Option. |
| Pattern Matching | Destructuring → match and let patterns: struct/enum/tuple/reference patterns and compile-time exhaustiveness. |
Methods and impl Blocks | Class methods → impl blocks: &self / &mut self / self, and splitting behavior across multiple impl blocks. |
| Associated Functions and Constructors | static methods → associated functions: Self::new constructors and a builder-pattern teaser. |
| Associated Types and Associated Constants | A light introduction to associated types and associated consts (full treatment in Section 09). |
| Field Init Shorthand and Struct Update Syntax | Object property shorthand → field init shorthand, plus ..other struct update syntax. |
Learning Objectives
Section titled “Learning Objectives”By the end of this section, you will be able to:
- Choose the right modeling tool for a given shape — struct, tuple struct, unit struct, or enum — and justify why
- Translate a TypeScript
interface,typealias, or discriminated union into an idiomatic, nominally typed Rust equivalent - Replace
null/undefinedand?./??chains withOption<T>, its combinators, and the?operator - Destructure structs, enums, tuples, and slices with
match,if let,let ... else, andwhile let, and rely on exhaustiveness to catch missed cases at compile time - Attach behavior to your types with
implblocks, choosing&self,&mut self, orselfdeliberately - Write
Self::newand named alternative constructors with associated functions, and recognize when a builder is warranted - Use field init shorthand and
..otherstruct update syntax to write concise, readable instantiation
Prerequisites
Section titled “Prerequisites”- Section 05: Ownership — structs own their fields, methods borrow or consume
self, and matching can move or borrow; this section assumes you are comfortable with moves, borrows, andClone - Section 02: Basics — concrete types (
u64,f64,bool,String), immutability-by-default, and#[derive(...)]-style printing with{:?} - Section 04: Control Flow —
if/elseand loops, which pattern matching builds on and often replaces
Estimated Time
Section titled “Estimated Time”- Reading: 4-5 hours
- Hands-on Practice: 3-4 hours
- Exercises: 2-3 hours
- Total: 10-12 hours
Tip: Read the topics in order. Structs, tuple structs, and enums define the shapes of data;
Option<T>and pattern matching are how you work with those shapes; andimplblocks plus associated functions attach behavior. The biggest mental shift for a TypeScript developer is that Rust is nominally typed andmatchis exhaustive — two same-shaped types are not interchangeable, and the compiler will not let you forget a case.
Next: Section 07: Collections → — Vec, HashMap, HashSet, and strings, the container types your structs and enums will live inside.