Share E-Book
Scan to open this page

Scan with your phone to open this page

Author: Jim Blandy, Jason Orendorff

Rust is a new systems programming language that combines the performance and low-level control of C and C++ with memory safety and thread safety. Rustâ s modern, flexible types ensure your program is free of null pointer dereferences, double frees, dangling pointers, and similar bugs, all at compile time, without runtime overhead. In multi-threaded code, Rust catches data races at compile time, making concurrency much easier to use. Written by two experienced systems programmers, this book explains how Rust manages to bridge the gap between performance and safety, and how you can take advantage of it. Topics include: How Rust represents values in memory (with diagrams) Complete explanations of ownership, moves, borrows, and lifetimes Cargo, rustdoc, unit tests, and how to publish your code on crates.io, Rustâ s public package repository High-level features like generic code, closures, collections, and iterators that make Rust productive and flexible Concurrency in Rust: threads, mutexes, channels, and atomics, all much safer to use than in C or C++ Unsafe code, and how to preserve the integrity of ordinary code that uses it Extended examples illustrating how pieces of the language fit together

AI Reading Assistant

Whole-book reading guide from stratified index samples; jump to passages in the text

AI guide
# Programming Rust: Fast, Safe Systems Development ## 【One-Line Pitch】 A comprehensive, deeply technical guide to Rust for experienced systems programmers who want to understand how the language achieves memory safety and thread safety without sacrificing performance, covering everything from ownership and lifetimes to concurrency and unsafe code. ## 【Book Arc】 - **Opening (~0%–9%)**: Establishes Rust's core value proposition—combining C/C++ performance with compile-time memory and thread safety—and introduces the fundamental concept of type safety versus undefined behavior, contrasting Rust with C, C++, and Python. - **Early (~9%–25%)**: A practical tour of Rust through working examples (GCD calculator, Mandelbrot set), introducing Cargo, basic syntax, structs, generics, and the standard library, while building intuition for how Rust represents values in memory. - **Early (~25%–34%)**: Deep dive into basic types—integers, floats, arrays, slices, strings, and vectors—with precise range tables and memory layout details, establishing the foundation for understanding ownership. - **Middle (~34%–47%)**: The heart of the book: ownership, moves, borrows, and lifetimes. Explains why Rust's move semantics differ from C++ and Python, how Copy types work, and how exclusive mutable references prevent entire classes of bugs including data races. - **Middle (~47%–end of sample)**: Continues with references and lifetimes in depth, showing how lifetime parameters in type definitions reveal borrowing relationships, and introduces control flow constructs (if let, while let, loops) that work with Rust's pattern matching. ## 【Key Takeaways】 - **Type safety is Rust's core promise** (Opening): Unlike C and C++, Rust guarantees that well-typed programs never exhibit undefined behavior—null dereferences, double frees, and dangling pointers are caught at compile time, not runtime. This eliminates entire categories of security vulnerabilities. - **Rust's type system is the safety mechanism** (Early): The language doesn't rely on runtime checks or garbage collection; instead, compile-time analysis of how values are used ensures memory safety with zero runtime overhead, making it suitable for systems programming. - **Move semantics make costs explicit** (Middle): Rust's assignment and parameter passing perform shallow byte-for-byte copies that leave the source uninitialized, unlike C++'s customizable copy/move constructors. This makes expensive operations like deep copies (via `clone`) explicit and predictable. - **Copy types are a deliberate design choice** (Middle): Types that implement `Copy` (like integers) are more flexible in usage but severely limited in what they can contain—heap-allocated data like `String` cannot be `Copy`, forcing programmers to think about ownership transfer. - **Exclusive mutable references prevent whole bug classes** (Middle): Rust's rule that mutable access must be exclusive eliminates self-assignment bugs, invalidated iterator issues, and overlapping `memcpy` problems—all manifestations of the same underlying mistake that C++ programmers encounter regularly. - **Data races are impossible by construction** (Middle): Because shared and mutable references cannot coexist, concurrent Rust code that avoids `unsafe` is free from data races at compile time, making concurrency dramatically safer than in C or C++. - **Lifetime parameters reveal borrowing relationships** (Middle): A type's lifetime parameters expose whether it contains references with non-static lifetimes, making the borrowing structure of complex types visible and checkable at compile time. ## 【Reading Tips】 - **Skim the opening chapters** (~0%–9%) if you're already convinced Rust is worth learning; the value proposition and type safety discussion are useful but not where the practical content lives. - **Deep-read the ownership and references chapters** (~34%–47%): This is the conceptual core of Rust and the hardest material for newcomers. Work through the memory diagrams carefully—they're essential for understanding why moves and borrows work the way they do. - **Use the early examples as hands-on practice**: The GCD calculator and Mandelbrot set programs (Early chapters) are excellent for getting comfortable with Cargo, basic syntax, and the standard library before tackling harder concepts. - **Pay attention to the C++ comparisons**: The book frequently contrasts Rust with C++ behavior (assignment, references, constructors). If you know C++, these comparisons accelerate understanding; if not, they're still illuminating but can be skimmed. - **Don't skip the type tables** (Early, ~25%): The integer ranges and memory layout details matter for systems programming, but you can return to them as reference material rather than memorizing upfront. ## 【Coverage Limits】 The excerpts cover roughly the first half of the book (through references and control flow). Topics like generics, traits, closures, iterators, collections, concurrency in depth, and unsafe code are mentioned but not covered in the available material. ##
Page 3
429 18. Input and Output. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 431 Readers and W...
View in text
Excerpt 2
stalls a copy on your computer when you install Rust itself. You can view the standard library documenta‐ tion in your browser with the command: $ rustup doc...
View in text
Excerpt 3
etail. For now it will suffice to point out that a &str can refer to any slice of any string, whether it is a string literal (stored in the executable) or a...
View in text
Excerpt 4
It turns out that two classic C++ bugs—failure to cope with self-assignment, and using invalidated iterators—are the same underlying kind of bug! In both cas...
View in text
Excerpt 5
n recursively. For example, the source code for version 0.6.1 of the image crate contains a Cargo.toml file that includes this: [dependencies] byteorder = "0...
View in text
Excerpt 6
orrow parts of a matched value. & patterns match references. We’ll cover ref patterns first. Matching on a noncopyable value moves the value. Continuing with...
View in text
Excerpt 7
rt("百", 100); m.insert("千", 1000); m.insert("万", 1_0000); m.insert("億", 1_0000_0000); assert_eq!(m["十"], 10); assert_eq!(m["千"], 1000); Those indexing expres...
View in text
Excerpt 8
e that Rust’s function val‐ ues are exactly the same thing. After all this, it may come as a surprise that closures do not have the same type as functions: l...
View in text
Tags
AI categories
Programming LanguageBackendTechnology
ISBN: 1491927283
Publisher: O’Reilly Media
Publish Year: 2017
Language: English
Pages: 622
File Format: PDF
File Size: 7.0 MB
Text Preview (First 20 pages)
Registered users can read the full content for free

Register as a Gaohf Library member to read the complete e-book online for free and enjoy a better reading experience.

Generating text preview…