Rust
Ownership, borrowing and errors as values.
The course
Open the courseStart at 01. The first lesson names the types. The next one builds an object. Each sample is on the page.
-
Skill
Basics
The types, explained, then an object you can name and call.
-
01
Rust from zero: a name has a type
Start here
A Rust name has a type. name is a string slice, cents is an i32, and ready is a bool. The program prints latte $4.50 ready.
Adds The types this course uses
-
02
Rust from zero: a struct is the object
A struct groups fields, and impl adds a method. Item holds a drink and a price in cents. label prints latte $4.50.
Adds An object you can name and call
-
01
Rust from zero: a name has a type
Start here
-
Practice
Tips
A rule that prevents a bug, then a program that uses the object.
-
03
Rust from zero: clone when two names need the text
Assigning a String moves it. The old name cannot use it. clone keeps both. The program prints latte latte.
Adds A tip that prevents a bug
-
04
Rust from zero: a method can change the struct
A method with &mut self changes the struct. add_tip adds 20 percent to a $4.50 bill, and the program prints $5.40.
Adds A program that uses the object
-
03
Rust from zero: clone when two names need the text
-
Further
A whole program
One more idea, then a program that puts the pieces together.
-
05
Rust from zero: an enum names the cases
An enum lists the shapes a value can have. Paid is Yes with the cents, or No. The program prints paid 450 and unpaid.
Adds A program that puts the pieces together
-
06
Rust from zero: a trait is a method list
A trait names the methods a function needs. Label asks for label. Item implements it. show prints latte $4.50.
Adds A program that puts the pieces together
-
05
Rust from zero: an enum names the cases
GoByte Skills: Rust track, 3 lessons. The ? operator, ownership and the borrow checker: memory safety decided at compile time.
Open the Rust trackArticles
RSS-
The borrow checker: many readers or one writer
GoByte Skills #12: Rust lets you hold many shared references or one mutable one, never both. That single rule is why push on a borrowed Vec does not compile, and why two threads cannot write the same variable without a lock or an atomic.
-
Ownership: one owner, no garbage collector
GoByte Skills #11: Rust frees memory without a garbage collector because every value has exactly one owner. Move it and the old name is dead. When the owner's scope ends, the compiler has already written the drop call.
-
The ? operator: errors are values
GoByte Skills #10: The ? operator is a match you did not have to type. On Err it converts the error through From and returns early, so the error changes type on the way up and never leaves the signature. No exceptions, no unwinding.