Safari, the world’s most comprehensive technology and business learning platform.

Find the exact information you need to solve a problem on the fly, or go deeper to master the technologies and skills you need to succeed

Start Free Trial

No credit card required

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
    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
    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
      1. if let
    5. Loops
    6. return expressions and the ? operator
    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. Handling errors
    1. Panic
      1. Unwinding
      2. Aborting
    2. Result
      1. Catching errors
      2. Result type aliases
      3. Printing errors
      4. Propagating errors
      5. Working with multiple error types
      6. Dealing with errors that “can’t happen”
      7. Ignoring errors
      8. Handling errors in main()
      9. Declaring a custom error type
      10. Why Results?
  8. 8. 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
  9. 9. 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
  10. 10. 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
  11. 11. 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
  12. 12. 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
  13. 13. 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
  14. 14. 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
  15. 15. 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
  16. 16. Input and output
    1. Readers and writers
      1. Readers
      2. Buffered readers
      3. Reading lines
      4. Collecting lines
      5. Writers
      6. Files
      7. Seeking
      8. Other reader and writer types
      9. Binary data, compression, and serialization
    2. Files and directories
      1. OsStr and Path
      2. Path and PathBuf methods
      3. Working with files and directories
      4. Reading directories
      5. Platform-specific features
    3. Networking
  17. 17. 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