Skip to main content

Programming Rust, 3rd Edition

Technical Books
In Progress
My notes on Programming Rust, 3rd Edition by Jim Blandy, Jason Orendorff, and Leonora F. S. Tindall
Author

Tyler Hillery

Published

November 6, 2026


Notes

Preface

No Notes.

Chapter 01: Systems Programmers Can Have Nice Things

No notes.

Chapter 02: A Tour of Rust

  • Man, right off the rip I love how everything is built into cargo. No messing around with make files. cargo new program, cargo run, cargo clean love it.
  • #[test] is an example of an attribute. Attributes are an open-ended system for marking functions and other declarations with extra information. They’re used to control compiler warnings and code style checks, include code conditionally (like #ifdef in C) and tell Rust how to interact with code written in other languages.
  • assert_eq! is a macro, ! character marks macro calls and are “expanded” to Rust code at compile time.
  • A trait is a collection of function signatures that types can implement. Any type that implements the FromStr trait has a from_str method that tries to prase a value of that type from a string.
NoteTraits Must Be in Scope

A trait must be in scope to use its methods even though you never the trait name anywhere else in the program.

  • What is interesting as well from u64 is that it’s a type that implements a method to conform to the FromStr trait. Think of this similar to static methods on classes in traditional OOP.
NoteAside

The from_str function doesn’t return a u64 directly, but rather a Result value that indicates whether the parse succeeded or failed. A Result value is one of two variants:

  • A value written Ok(v), indicating that the parse succeeded and v is the value produced
  • A value written Err(e), indicating that the parse failed and e is an error value explaining why

Functions that do anything that might fail, such as doing input or output or otherwise interacting with the operating system, can return Result types whose Ok variants carry successful results—the count of bytes transferred, the file opened, and so on—and whose Err variants carry an error code indicating what went wrong. Unlike most modern languages, Rust does not have exceptions: all errors are handled using either Result or panic, as outlined in Chapter 7.

I have heard people rave about how Rust deals with errors.

  • expect() is a method on the Result type where if the result is Err(e) it will print a message that includes a description of e and exits the program. Otherwise if the result is Ok(v) is simply returns v itself.
  • &numbers[1..] borrows a reference to the vector’s elements from the second onward. *m dereferences m, yielding the value it refers to. Since numbers owns the vector, Rust automatically frees it when numbers goes out fo scope at the end of main.
NoteAside

I have heard about “fighting the borrow checker” in Rust. This smaller example still isn’t quite clear to me how ownership works in Rust but I know it’s core to how Rust enforces memory safety. Looking forward to Chapters 4 and 5 where the authors will go into more details.

  • || is Rust closure expression. A closure is a value that can be called as if it were a function.

  • I think my biggest takeaway so from Rust is that it takes the unspoken rules and general guidelines from C & C++ and makes those rules explicit and enforced by the compiler so the programmers intent on how to use something is enforced.

  • Okay Rust enums were tripping me up at first because I was treating None as a specific Rust keyword but it’s a variant in the Option<T> which is a generic enum type takes in a type T and returns either None or Some(T). Now Some was confusing to me because wonder why can’t I just do None or T thinking in terms of TypeScript unions like null | T. This wasn’t the right way to think of about it. Instead it’s more like a TypeScript discriminated union e.g.

    type Option<T> {
        | { kind: "None"}
        | { kind: "Some"; value: T};
    }

    This made it click for me.

  • The unit type for Rust () is akin to void in C

  • ahhhhh so ? is just syntax sugar for

    let output = match File::create(filename) {
        Ok(f) => f,
        Err(e) => {
            return Err(e);
        }
    };
    // you can just do
    let output = File::create(filename)?;

Chapter 03: Fundamental Types

  • usize and isize are analogous to size_t and ptrdiff_t in C.
  • Rust’s char type is a Unicode character type, not an integer type, and is quite different from C’s char. You can use u8 type for byte values.
  • When indexing into a slice you have to use a usize value
  • Array’s lengths are fixed at compile time. [0u8: 1024] is a one kb buffer filled with zeros.
  • Rust has no notation for an uninitialized array and in general rust ensures that code can never access uninitialized memory.
  • The benefit of arrays (given their limitations) is they are efficient: small arrays can be stored on the stack or inside a struct without allocating more heap memory.
  • For lists with variable length use a vector.
  • Vector’s contain three values:
    1. A pointer to a heap-allocated buffer
    2. number of elements that buffer has the capacity to store
    3. number of elements that is actually contains now (its length)
  • When the buffer has reached capacity adding another element to the vector entails allocating a larger buffer, copying the present contents into it, updating the vector’s pointer and capacity to describe the new buffer and freeing the old one.
  • Vec::with_capacity allows you specific the capacity at creation time.
  • Vec is an ordinary type defined in Rust and is not built into the language.
  • A slice written [T] without specifying the length, is a region of an array or vector.
  • Slices are always passed by reference.
  • A slice reference is a fat pointer: a two-word value comprising a pointer to the slice’s first element and the number of elements in the slice.

Chapter 04: Ownership and Moves

  • This is nice convenient method:

    ```rust let first_name = composers[0].name.take(); // same as let first_name = std::mem:replace(&mut composers[0].name, None);

  • Wow, it’s surprising to me hear that when you move ownership the original owner variable is actually uninitialized.

  • For user defined types like enum or structs you have to implement the Copy and Clone traits for the value to be copied instead of moved. If you type only includes values that are already copyable you can use the #[derive(Copy, Clone)] attribute where the compiler will generate the Rust code for you to implement this traits.

Chapter 05: References

  • Very interesting to see how Rust handles refs and derefs for you. I can traverse refs of refs of refs all the way to the value without making you deref. Works for comparison operators and struct field access.
  • If you do want to compare if two refs actually point to the same memory address you can use std::ptr::eq()
  • Key Rust ref rules:
    • can never be null
    • you can’t use any variable until it’s been initialized
    • can’t convert integers to references e.g. zero into a reference (outside of unsafe code)
    • In many places where a nullptr was traditional used Rust recommends using Option<&T>