Facebook Pixel
Searching...
English
EnglishEnglish
EspañolSpanish
简体中文Chinese
FrançaisFrench
DeutschGerman
日本語Japanese
PortuguêsPortuguese
ItalianoItalian
한국어Korean
РусскийRussian
NederlandsDutch
العربيةArabic
PolskiPolish
हिन्दीHindi
Tiếng ViệtVietnamese
SvenskaSwedish
ΕλληνικάGreek
TürkçeTurkish
ไทยThai
ČeštinaCzech
RomânăRomanian
MagyarHungarian
УкраїнськаUkrainian
Bahasa IndonesiaIndonesian
DanskDanish
SuomiFinnish
БългарскиBulgarian
עבריתHebrew
NorskNorwegian
HrvatskiCroatian
CatalàCatalan
SlovenčinaSlovak
LietuviųLithuanian
SlovenščinaSlovenian
СрпскиSerbian
EestiEstonian
LatviešuLatvian
فارسیPersian
മലയാളംMalayalam
தமிழ்Tamil
اردوUrdu
Programming Rust

Programming Rust

Fast, Safe Systems Development
by Jim Blandy 2021 735 pages
4.63
500+ ratings
Listen

Key Takeaways

1. Rust's Ownership System: Memory Safety Without Garbage Collection

Rust's radical wager is that even with these restrictions in place, you'll find the language more than flexible enough for almost every task, and that the benefits—the elimination of broad classes of memory management and concurrency bugs—will justify the adaptations you'll need to make to your style.

Ownership and borrowing. Rust's ownership system is a novel approach to memory management that ensures memory safety without the need for garbage collection. Every value in Rust has a single owner, and ownership can be transferred (moved) between variables. When a value goes out of scope, Rust automatically frees its memory.

References and lifetimes. Rust allows borrowing references to values, which can be either shared (&T) or mutable (&mut T). The borrow checker enforces strict rules to prevent data races and use-after-free errors:

  • Only one mutable reference OR any number of shared references to a value at a time
  • References must not outlive their referents

These rules are enforced at compile-time, eliminating entire classes of bugs without runtime overhead.

2. Structs and Enums: Building Blocks for Custom Types

Just as everywhere else in the language, if a closure would move a value of a copyable type, like i32, it copies the value instead.

Structs for data grouping. Structs in Rust allow you to create custom types by grouping related data together. They come in three flavors:

  • Named-field structs: Most common, with named fields
  • Tuple structs: Unnamed fields, useful for simple wrappers
  • Unit-like structs: No fields, useful for implementing traits

Enums for variants. Enums in Rust are more powerful than in many other languages, allowing you to define a type that can be one of several variants. Each variant can optionally hold data, making enums ideal for representing states or results that can take on multiple forms.

Rust's pattern matching capabilities, particularly with the match expression, make working with enums highly ergonomic and safe.

3. Traits: Defining Shared Behavior

Traits are Rust's take on interfaces or abstract base classes.

Defining shared behavior. Traits in Rust define a set of methods that types can implement, similar to interfaces in other languages. They allow for abstraction over behavior that types can share, enabling polymorphism and code reuse.

Trait objects and generics. Rust offers two main ways to use traits:

  1. Trait objects: Allow for runtime polymorphism with some performance overhead
  2. Generic traits: Provide zero-cost abstractions through monomorphization

Traits can also have associated types and default method implementations, making them a powerful tool for designing flexible and reusable code.

4. Error Handling: Results and Panics

Rust doesn't have exceptions. Instead, functions that can fail have a return type that says so.

Results for expected errors. Rust uses the Result<T, E> type to handle expected errors. This enum has two variants:

  • Ok(T): Holds the successful result
  • Err(E): Contains the error information

The ? operator provides syntactic sugar for propagating errors, making error handling concise and explicit.

Panics for unexpected errors. For unexpected errors or situations where recovery is impossible, Rust uses panics. Panics unwind the stack, calling destructors and freeing resources. They're used for:

  • Programming errors (e.g., out-of-bounds access)
  • Situations where continuing is impossible or dangerous

This two-pronged approach encourages handling expected errors gracefully while still providing a mechanism for unrecoverable situations.

5. Modules and Crates: Organizing and Sharing Code

Rust programs are made of crates.

Modules for code organization. Modules in Rust provide a way to organize code within a crate. They can be:

  • Defined in a single file
  • Spread across multiple files
  • Nested within other modules

Modules control the privacy of items, allowing you to define public interfaces while keeping implementation details private.

Crates for code sharing. Crates are the unit of compilation and distribution in Rust:

  • Library crates: Provide functionality to be used by other crates
  • Binary crates: Produce executables

The Cargo package manager makes it easy to manage dependencies, build projects, and publish crates to crates.io, the Rust community's package registry.

6. Iterators: Powerful Abstractions for Sequences

Rust's iterators were carefully designed to ensure that the compiler can translate them into excellent machine code as well.

Lazy evaluation. Iterators in Rust are lazy, meaning they only perform work when consumed. This allows for efficient chaining of operations without creating intermediate collections.

Powerful methods. The Iterator trait provides a rich set of methods for working with sequences:

  • Adapters: Transform iterators (e.g., map, filter, zip)
  • Consumers: Produce final results (e.g., collect, fold, sum)

These methods often lead to more readable and efficient code compared to explicit loops.

Zero-cost abstraction. Through compiler optimizations, iterator chains often compile down to code as efficient as hand-written loops, providing high-level abstractions without performance penalties.

7. Closures: Flexible and Efficient Function-Like Types

Rust closures are designed to be fast: faster than function pointers, fast enough that you can use them even in red-hot, performance-sensitive code.

Syntax and capture. Closures in Rust are anonymous functions that can capture values from their environment. They have a concise syntax and can capture by:

  • Reference: Borrows values
  • Mutable reference: Borrows values mutably
  • Value: Takes ownership of values (using the move keyword)

Closure traits. Rust defines three traits for closures:

  1. Fn: Can be called multiple times without modifying captured values
  2. FnMut: Can modify captured values
  3. FnOnce: Consumes captured values, can only be called once

These traits allow for fine-grained control over closure behavior and enable efficient implementations.

8. Concurrency: Safe and Efficient Parallelism

A concurrent Rust program that avoids unsafe code is free of data races by construction.

Fearless concurrency. Rust's ownership and type systems prevent data races at compile-time, making concurrent programming much safer than in many other languages.

Threading primitives. Rust provides several tools for concurrent programming:

  • std::thread: For spawning OS-level threads
  • std::sync: Synchronization primitives (e.g., Mutex, Arc)
  • Channels: For message passing between threads

Async/await. Rust also supports asynchronous programming with its async/await syntax, allowing for efficient handling of many concurrent tasks without the overhead of OS threads.

9. Smart Pointers: Advanced Memory Management

Rc and Arc: Shared Ownership

Beyond raw pointers. Rust provides several smart pointer types that add functionality beyond what raw pointers offer:

  • Box<T>: For heap allocation
  • Rc<T>: Reference-counted shared ownership
  • Arc<T>: Atomic reference-counted shared ownership (thread-safe)
  • RefCell<T>: For interior mutability

Safety and convenience. These smart pointers provide safe abstractions for common patterns in systems programming, such as:

  • Dynamic dispatch
  • Shared ownership
  • Interior mutability

They allow for more complex memory management scenarios while still maintaining Rust's safety guarantees.

Last updated:

Review Summary

4.63 out of 5
Average of 500+ ratings from Goodreads and Amazon.

Programming Rust receives overwhelmingly positive reviews, praised for its comprehensive coverage of Rust's unique features. Readers appreciate the book's in-depth explanations, particularly regarding memory management, concurrency, and systems programming concepts. Many found it helpful for both learning Rust and as a reference. The book is noted for its clear writing style, useful examples, and comparisons to C/C++. While some sections are complex, most readers found the content engaging and valuable. It's highly recommended for experienced programmers looking to master Rust.

Your rating:

About the Author

Jim Blandy is a respected author and programmer known for his expertise in systems programming languages. His book "Programming Rust" has been widely acclaimed for its thorough and accessible approach to teaching the Rust programming language. Blandy's background in C and C++ development provides valuable insights into Rust's advantages and practical applications. His writing style is praised for its clarity and depth, making complex concepts understandable to experienced programmers. Blandy's work has contributed significantly to the Rust community, helping developers transition from other languages and master Rust's unique features. His expertise in systems programming and language design is evident throughout the book.

Download PDF

To save this Programming Rust summary for later, download the free PDF. You can print it out, or read offline at your convenience.
Download PDF
File size: 0.24 MB     Pages: 10

Download EPUB

To read this Programming Rust summary on your e-reader device or app, download the free EPUB. The .epub digital book format is ideal for reading ebooks on phones, tablets, and e-readers.
Download EPUB
File size: 2.96 MB     Pages: 8
0:00
-0:00
1x
Dan
Andrew
Michelle
Lauren
Select Speed
1.0×
+
200 words per minute
Create a free account to unlock:
Bookmarks – save your favorite books
History – revisit books later
Ratings – rate books & see your ratings
Unlock unlimited listening
Your first week's on us!
Today: Get Instant Access
Listen to full summaries of 73,530 books. That's 12,000+ hours of audio!
Day 4: Trial Reminder
We'll send you a notification that your trial is ending soon.
Day 7: Your subscription begins
You'll be charged on Nov 22,
cancel anytime before.
Compare Features Free Pro
Read full text summaries
Summaries are free to read for everyone
Listen to summaries
12,000+ hours of audio
Unlimited Bookmarks
Free users are limited to 10
Unlimited History
Free users are limited to 10
What our users say
30,000+ readers
“...I can 10x the number of books I can read...”
“...exceptionally accurate, engaging, and beautifully presented...”
“...better than any amazon review when I'm making a book-buying decision...”
Save 62%
Yearly
$119.88 $44.99/yr
$3.75/mo
Monthly
$9.99/mo
Try Free & Unlock
7 days free, then $44.99/year. Cancel anytime.
Settings
Appearance