You are previewing Programming Rust.
O'Reilly logo
Programming Rust

Book Description

This practical book introduces systems programmers to Rust, the new and cutting-edge language. You’ll learn how Rust has the potential to be the first usable programming language that brings the benefits of an expressive modern type system to systems programming. Author Jim Blandy presents Rust’s rules clearly and economically; elaborates on their consequences; and shows you how to express the programs you want to write in terms that Rust can prove are free of broad classes of everyday errors.

Table of Contents

  1. 1. Why Rust?
    1. Type Safety
  2. 2. A Tour of Rust
    1. Downloading and installing Rust
    2. A simple function
    3. Writing and running unit tests
    4. Handling command-line arguments
    5. A simple web server
    6. Concurrency
      1. What the Mandelbrot set actually is
      2. Parsing pair command-line arguments
      3. Mapping from pixels to complex numbers
      4. Plotting the set
      5. Writing bitmap files
      6. A concurrent Mandelbrot program
      7. Running the Mandelbrot plotter
      8. Safety is invisible
  3. 3. Basic types
    1. Machine types
      1. Integer types
      2. Floating-point types
      3. The bool type
      4. Characters
    2. Tuples
    3. Pointer types
      1. References
      2. Boxes
      3. Raw pointers
    4. Arrays, Vectors, and Slices
      1. Arrays
      2. Vectors
      3. Slices
    5. String types
      1. String literals
      2. Byte strings
      3. Strings in memory
      4. String
      5. Using strings
      6. Other string-like types
    6. Beyond the basics
  4. 4. Ownership and moves
    1. Ownership
    2. Moves
      1. More operations that move
      2. Moves and control flow
      3. Moves and indexed content
    3. Copy types: the exception to moves
    4. Rc and Arc: shared ownership
  5. 5. References and borrowing
    1. References as values
      1. Rust references versus C++ references
      2. Assigning references
      3. References to references
      4. Comparing references
      5. References to slices and trait objects
      6. References are never null
      7. Borrowing references to arbitrary expressions
    2. Reference safety
      1. Borrowing a local variable
      2. Receiving references as parameters
      3. Passing references as arguments
      4. Returning references
      5. Structs containing references
      6. Distinct lifetime parameters
    3. Sharing versus mutation
  6. 6. Expressions
    1. An expression language
    2. Blocks and statements
    3. Declarations
    4. if and match
    5. Loops
    6. return expressions
    7. Why Rust has loop
    8. Function and method calls
    9. Fields and elements
    10. Reference operators
    11. Arithmetic, bitwise, comparison, and logical operators
    12. Assignment
    13. Type casts
    14. Closures
    15. Precedence and associativity
    16. Onward
  7. 7. Program structure
    1. Crates
    2. Modules
      1. Modules in separate files
      2. Paths and imports
      3. The standard prelude
      4. Items, the building blocks of Rust
    3. Turning a program into a library
    4. The src/bin directory
    5. Attributes
    6. Tests and documentation
      1. Integration tests
      2. Documentation
      3. Doc-tests
    7. Specifying dependencies
      1. Versions
      2. Cargo.lock
    8. Publishing crates to crates.io
  8. 8. Structs
    1. Named-field structs
    2. Tuple-like structs
    3. Unit-like structs
    4. Struct layout
    5. Defining methods with impl
    6. Generic structs
    7. Structs with lifetime parameters
    8. Deriving common traits for struct types
      1. Copy and Clone
      2. Debug: printing struct values
      3. PartialEq: checking structs for equality
      4. PartialOrd: ordering structs
  9. 9. Enums and patterns
    1. Enums
      1. Tuple and struct variants
      2. Enums in memory
      3. Rich data structures using enums
      4. Generic enums
    2. Patterns
      1. Tuple and struct patterns
      2. Reference patterns
      3. Matching multiple possibilities
      4. Pattern guards
      5. @ patterns
      6. Where patterns are allowed
      7. Populating a binary tree
    3. The big picture
  10. 10. Traits and generics
    1. Using traits
      1. Trait objects
      2. Generic functions
      3. Which to use
    2. Defining and implementing traits
      1. Default methods
      2. Traits and other people’s types
      3. Self in traits
      4. Subtraits
      5. Static methods
    3. Traits and related types
      1. Associated types (or, how iterators work)
      2. Generic traits (or, how operator overloading works)
      3. Buddy traits (or, how rand::random() works)
    4. Reverse-engineering bounds
    5. Conclusion
  11. 11. Built-in and utility traits
    1. Built-in traits for arithmetic and bitwise operators
      1. Unary operators
      2. Binary operators
      3. Compound assignment operators
    2. Built-in traits for equality tests
    3. Built-in traits for ordered comparisons
    4. Built-in traits for iteration
    5. Drop
    6. Deref and DerefMut
    7. Index and IndexMut
    8. Fn, FnMut, and FnOnce
    9. Sized
    10. Clone
    11. Copy
    12. Send and Sync
    13. Default
    14. AsRef and AsMut
    15. Borrow and BorrowMut
    16. From and Into
    17. ToOwned
    18. Cow
  12. 12. Closures
    1. Closures and scope
      1. Closures that borrow
      2. Closures that steal
    2. Function and closure types
    3. Closure performance
    4. Closures that kill
    5. FnOnce
    6. FnMut
    7. Callbacks
    8. Using closures effectively
  13. 13. Iterators
    1. The Iterator and IntoIterator traits
    2. Obtaining iterators
      1. iter and iter_mut methods
      2. IntoIterator implementations
      3. drain methods
      4. Other iterator sources
    3. Iterator adapters
      1. map and filter
      2. filter_map and flat_map
      3. take and take_while
      4. skip and skip_while
      5. peekable
      6. Reversible iterators and rev
      7. inspect
      8. chain
      9. enumerate
      10. zip
      11. by_ref
      12. cloned
      13. cycle
    4. Consuming iterators
      1. Simple accumulation: count, sum, product
      2. max, min
      3. max_by_key, min_by_key
      4. any and all
      5. fold
      6. nth
      7. find
      8. Building collections: collect and FromIterator
      9. The Extend trait
      10. partition
    5. Implementing your own iterators
  14. 14. Collections
    1. Overview
    2. Vec<T>
      1. Accessing elements
      2. Iteration
      3. Growing and shrinking vectors
      4. Joining
      5. Splitting
      6. Swapping
      7. Sorting and searching
      8. Comparing slices
    3. Random elements
      1. Rust rules out invalidation errors
    4. VecDeque<T>
    5. LinkedList<T>
    6. BinaryHeap<T>
    7. Maps
      1. Entries
      2. Map iteration
    8. Sets
      1. Set iteration
      2. When equal values are different
      3. Whole-set operations
    9. Hashing
      1. Using a custom hashing algorithm
    10. Building custom collections
  15. 15. Concurrency
    1. Fork-join parallelism
      1. spawn and join
      2. Error handling across threads
      3. Sharing immutable data across threads
      4. Rayon
      5. Revisiting the Mandelbrot set
    2. Channels
      1. Sending values
      2. Receiving values
      3. Running the pipeline
      4. Channel features and performance
      5. Thread safety: Send and Sync
      6. Piping almost any iterator to a channel
      7. Beyond pipelines
    3. Shared mutable state
      1. What is a mutex?
      2. Mutex in Rust
      3. mut and Mutex
      4. Why mutexes are not always a good idea
      5. Deadlock
      6. Poisoned mutexes
      7. Multi-producer channels using Mutex
      8. RwLock
      9. Condvar
      10. Atomics
      11. Global variables
    4. What hacking concurrent code in Rust is like