Rust

Ownership, borrowing and errors as values.

The course

Open the course

Start at 01. The first lesson names the types. The next one builds an object. Each sample is on the page.

  1. Skill

    Basics

    The types, explained, then an object you can name and call.

    1. 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

      2 min read

    2. 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

      Builds on 12 min read

  2. Practice

    Tips

    A rule that prevents a bug, then a program that uses the object.

    1. 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

      Builds on 1 through 22 min read

    2. 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

      Builds on 1 through 32 min read

  3. Further

    A whole program

    One more idea, then a program that puts the pieces together.

    1. 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

      Builds on 1 through 42 min read

    2. 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

      Builds on 1 through 52 min read

GoByte Skills: Rust track, 3 lessons. The ? operator, ownership and the borrow checker: memory safety decided at compile time.

Open the Rust track

Articles

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.