O'Reilly logo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

The Rust Programming Language

Book Description

"The Rust Programming Language is the official book on Rust; a community-developed, systems programming language that runs blazingly fast, prevents segfaults, and guarantees thread safety. Rust’s memory safety guarantees, enforced at compile time, safeguard your programs against the many problems that pervade other systems languages. Rust offers the control and performance of a low-level language with the helpful abstractions of a high level one, and does this all without having a garbage collector. These characteristics make Rust useful for embedding in other languages, programs with specific space and time requirements, and writing low-level code, like device drivers and operating systems.

The Rust Programming Language begins with a quick hands-on project to introduce the basics, then explores key concepts in depth, such as ownership, the type system, error handling, and fearless concurrency. Detailed explanations of Rust-oriented takes on topics like pattern matching, iterators, and smart pointers combine with examples and exercises to take you from theory to practice.

In addition to its thorough coverage of more granular topics, The Rust Programming Language will show you how to:

• Grasp important concepts unique to Rust like ownership, borrowing, and lifetimes• Use Cargo, Rust’s built-in package manager, to build your code, including downloading and building dependencies• Effectively use Rust’s zero-cost abstractions and learn to build your own

Developed with help from the community, The Rust Programming Language is your official guide to becoming a productive Rust programmer."

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. ACKNOWLEDGMENTS
  9. 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
  10. 1 GETTING STARTED
    1. Installation
    2. Hello, World!
    3. Hello, Cargo!
    4. Summary
  11. 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
  12. 3 COMMON PROGRAMMING CONCEPTS
    1. Variables and Mutability
    2. Data Types
    3. Functions
    4. Comments
    5. Control Flow
    6. Summary
  13. 4 UNDERSTANDING OWNERSHIP
    1. What Is Ownership?
    2. References and Borrowing
    3. The Slice Type
    4. Summary
  14. 5 USING STRUCTS TO STRUCTURE RELATED DATA
    1. Defining and Instantiating Structs
    2. An Example Program Using Structs
    3. Method Syntax
    4. Summary
  15. 6 ENUMS AND PATTERN MATCHING
    1. Defining an Enum
    2. The match Control Flow Operator
    3. Concise Control Flow with if let
    4. Summary
  16. 7 USING MODULES TO REUSE AND ORGANIZE CODE
    1. mod and the Filesystem
    2. Controlling Visibility with pub
    3. Referring to Names in Different Modules
    4. Summary
  17. 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
  18. 9 ERROR HANDLING
    1. Unrecoverable Errors with panic!
    2. Recoverable Errors with Result
    3. To panic! or Not to panic!
    4. Summary
  19. 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
  20. 11 WRITING AUTOMATED TESTS
    1. How to Write Tests
    2. Controlling How Tests Are Run
    3. Test Organization
    4. Summary
  21. 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
  22. 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
  23. 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
  24. 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
  25. 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
  26. 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
  27. 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
  28. 19 ADVANCED FEATURES
    1. Unsafe Rust
    2. Advanced Lifetimes
    3. Advanced Traits
    4. Advanced Types
    5. Advanced Functions and Closures
    6. Summary
  29. 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
  30. A KEYWORDS
    1. Keywords Currently in Use
    2. Keywords Reserved for Future Use
  31. B OPERATORS AND SYMBOLS
    1. Operators
    2. Non-operator Symbols
  32. 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
  33. D MACROS
    1. The Difference Between Macros and Functions
    2. Declarative Macros with macro_rules! for General Metaprogramming
    3. Procedural Macros for Custom derive
    4. The Future of Macros
  34. INDEX