The Rust Programming Language (Covers Rust 2018)

Book description

The Rust Programming Language is the official book on Rust: an open source systems programming language that helps you write faster, more reliable software. Rust offers control over low-level details (such as memory usage) in combination with high-level ergonomics, eliminating the hassle traditionally associated with low-level languages.

The authors of The Rust Programming Language, members of the Rust Core Team, share their knowledge and experience to show you how to take full advantage of Rust’s features—from installation to creating robust and scalable programs. You’ll begin with basics like creating functions, choosing data types, and binding variables and then move on to more advanced concepts, such as:

•Ownership and borrowing, lifetimes, and traits
•Using Rust’s memory safety guarantees to build fast, safe programs
•Testing, error handling, and effective refactoring
•Generics, smart pointers, multithreading, trait objects, and advanced pattern matching
•Using Cargo, Rust’s built-in package manager, to build, test, and document your code and manage dependencies
•How best to use Rust’s advanced compiler with compiler-led programming techniques

You’ll find plenty of code examples throughout the book, as well as three chapters dedicated to building complete projects to test your learning: a number guessing game, a Rust implementation of a command line tool, and a multithreaded server.

New to this edition: An extended section on Rust macros, an expanded chapter on modules, and appendixes on Rust development tools and editions.

Table of contents

  1. Cover Page
  2. Title Page
  3. Copyright Page
  4. About the Authors
  5. Brief Contents
  6. Contents in Detail
  7. FOREWORD by Nicholas Matsakis and Aaron Turon
  8. PREFACE
  9. ACKNOWLEDGMENTS
  10. INTRODUCTION
    1. Who Rust Is For
    2. Who This Book Is For
    3. How to Use This Book
    4. Resources and How to Contribute to This Book
  11. 1 GETTING STARTED
    1. Installation
    2. Command Line Notation
    3. Hello, World!
    4. Hello, Cargo!
    5. Summary
  12. 2 PROGRAMMING A GUESSING GAME
    1. Setting Up a New Project
    2. Processing a Guess
    3. Generating a Secret Number
    4. Comparing the Guess to the Secret Number
    5. Allowing Multiple Guesses with Looping
    6. Summary
  13. 3 COMMON PROGRAMMING CONCEPTS
    1. Variables and Mutability
    2. Data Types
    3. Functions
    4. Comments
    5. Control Flow
    6. Summary
  14. 4 UNDERSTANDING OWNERSHIP
    1. What Is Ownership?
    2. References and Borrowing
    3. The Slice Type
    4. Summary
  15. 5 USING STRUCTS TO STRUCTURE RELATED DATA
    1. Defining and Instantiating Structs
    2. An Example Program Using Structs
    3. Method Syntax
    4. Summary
  16. 6 ENUMS AND PATTERN MATCHING
    1. Defining an Enum
    2. The match Control Flow Operator
    3. Concise Control Flow with if let
    4. Summary
  17. 7 MANAGING GROWING PROJECTS WITH PACKAGES, CRATES, AND MODULES
    1. Packages and Crates
    2. Defining Modules to Control Scope and Privacy
    3. Paths for Referring to an Item in the Module Tree
    4. Bringing Paths into Scope with the use Keyword
    5. Separating Modules into Different Files
    6. Summary
  18. 8 COMMON COLLECTIONS
    1. Storing Lists of Values with Vectors
    2. Storing UTF-8 Encoded Text with Strings
    3. Storing Keys with Associated Values in Hash Maps
    4. Summary
  19. 9 ERROR HANDLING
    1. Unrecoverable Errors with panic!
    2. Recoverable Errors with Result
    3. To panic! or Not to panic!
    4. Summary
  20. 10 GENERIC TYPES, TRAITS, AND LIFETIMES
    1. Removing Duplication by Extracting a Function
    2. Generic Data Types
    3. Traits: Defining Shared Behavior
    4. Validating References with Lifetimes
    5. Generic Type Parameters, Trait Bounds, and Lifetimes Together
    6. Summary
  21. 11 WRITING AUTOMATED TESTS
    1. How to Write Tests
    2. Controlling How Tests Are Run
    3. Test Organization
    4. Summary
  22. 12 AN I/O PROJECT: BUILDING A COMMAND LINE PROGRAM
    1. Accepting Command Line Arguments
    2. Reading a File
    3. Refactoring to Improve Modularity and Error Handling
    4. Developing the Library’s Functionality with Test-Driven Development
    5. Working with Environment Variables
    6. Writing Error Messages to Standard Error Instead of Standard Output
    7. Summary
  23. 13 FUNCTIONAL LANGUAGE FEATURES: ITERATORS AND CLOSURES
    1. Closures: Anonymous Functions That Can Capture Their Environment
    2. Processing a Series of Items with Iterators
    3. Improving Our I/O Project
    4. Comparing Performance: Loops vs. Iterators
    5. Summary
  24. 14 MORE ABOUT CARGO AND CRATES.IO
    1. Customizing Builds with Release Profiles
    2. Publishing a Crate to Crates.io
    3. Cargo Workspaces
    4. Installing Binaries from Crates.io with cargo install
    5. Extending Cargo with Custom Commands
    6. Summary
  25. 15 SMART POINTERS
    1. Using Box<T> to Point to Data on the Heap
    2. Treating Smart Pointers Like Regular References with the Deref Trait
    3. Running Code on Cleanup with the Drop Trait
    4. Rc<T>, the Reference Counted Smart Pointer
    5. RefCell<T> and the Interior Mutability Pattern
    6. Reference Cycles Can Leak Memory
    7. Summary
  26. 16 FEARLESS CONCURRENCY
    1. Using Threads to Run Code Simultaneously
    2. Using Message Passing to Transfer Data Between Threads
    3. Shared-State Concurrency
    4. Extensible Concurrency with the Sync and Send Traits
    5. Summary
  27. 17 OBJECT-ORIENTED PROGRAMMING FEATURES OF RUST
    1. Characteristics of Object-Oriented Languages
    2. Using Trait Objects That Allow for Values of Different Types
    3. Implementing an Object-Oriented Design Pattern
    4. Summary
  28. 18 PATTERNS AND MATCHING
    1. All the Places Patterns Can Be Used
    2. Refutability: Whether a Pattern Might Fail to Match
    3. Pattern Syntax
    4. Summary
  29. 19 ADVANCED FEATURES
    1. Unsafe Rust
    2. Advanced Traits
    3. Advanced Types
    4. Advanced Functions and Closures
    5. Macros
    6. Summary
  30. 20 FINAL PROJECT: BUILDING A MULTITHREADED WEB SERVER
    1. Building a Single-Threaded Web Server
    2. Turning Our Single-Threaded Server into a Multithreaded Server
    3. Graceful Shutdown and Cleanup
    4. Summary
  31. A KEYWORDS
    1. Keywords Currently in Use
    2. Keywords Reserved for Future Use
    3. Raw Identifiers
  32. B OPERATORS AND SYMBOLS
    1. Operators
    2. Non-operator Symbols
  33. C DERIVABLE TRAITS
    1. Debug for Programmer Output
    2. PartialEq and Eq for Equality Comparisons
    3. PartialOrd and Ord for Ordering Comparisons
    4. Clone and Copy for Duplicating Values
    5. Hash for Mapping a Value to a Value of Fixed Size
    6. Default for Default Values
  34. D USEFUL DEVELOPMENT TOOLS
    1. Automatic Formatting with rustfmt
    2. Fix Your Code with rustfix
    3. More Lints with Clippy
    4. IDE Integration Using the Rust Language Server
  35. E EDITIONS
  36. INDEX

Product information

  • Title: The Rust Programming Language (Covers Rust 2018)
  • Author(s): Carol Nichols
  • Release date: July 2019
  • Publisher(s): No Starch Press
  • ISBN: 9781718500440