The Rust Programming Language. 2 Ed

The Rust Programming Language. 2 Ed

The Rust Programming Language. 2 Ed
Автор: Klabnik Steve, Nichols Carol
Дата выхода: 2023
Издательство: No Starch Press, Inc.
Количество страниц: 1220
Размер файла: 3,2 МБ
Тип файла: PDF
Добавил: codelibs
 Проверить на вирусы

Title Page
Copyright
About the Authors
Foreword
Preface
Acknowledgments
Introduction
Who Rust Is For
Teams of Developers
Students
Companies
Open Source Developers
People Who Value Speed and Stability
Who This Book Is For
How to Use This Book
Resources and How to Contribute to This Book

Chapter 1: Getting Started

Installation
Installing rustup on Linux or macOS
Installing rustup on Windows
Troubleshooting
Updating and Uninstalling
Local Documentation
Hello, World!
Creating a Project Directory
Writing and Running a Rust Program
Anatomy of a Rust Program
Compiling and Running Are Separate Steps
Hello, Cargo!
Creating a Project with Cargo
Building and Running a Cargo Project
Building for Release
Cargo as Convention
Summary

Chapter 2: Programming a Guessing Game

Setting Up a New Project
Processing a Guess
Storing Values with Variables
Receiving User Input
Handling Potential Failure with Result
Printing Values with println! Placeholders
Testing the First Part
Generating a Secret Number
Using a Crate to Get More Functionality
Generating a Random Number
Comparing the Guess to the Secret Number
Allowing Multiple Guesses with Looping
Quitting After a Correct Guess
Handling Invalid Input
Summary

Chapter 3: Common Programming Concepts

Variables and Mutability
Constants
Shadowing
Data Types
Scalar Types
Compound Types
Functions
Parameters
Statements and Expressions
Functions with Return Values
Comments
Control Flow
if Expressions
Repetition with Loops
Summary

Chapter 4: Understanding Ownership

What Is Ownership?
Ownership Rules
Variable Scope
The String Type
Memory and Allocation
Ownership and Functions
Return Values and Scope
References and Borrowing
Mutable References
Dangling References
The Rules of References
The Slice Type
String Slices
Other Slices
Summary

Chapter 5: Using Structs to Structure Related Data

Defining and Instantiating Structs
Using the Field Init Shorthand
Creating Instances from Other Instances with Struct Update Syntax
Using Tuple Structs Without Named Fields to Create Different Types
Unit-Like Structs Without Any Fields
An Example Program Using Structs
Refactoring with Tuples
Refactoring with Structs: Adding More Meaning
Adding Useful Functionality with Derived Traits
Method Syntax
Defining Methods
Methods with More Parameters
Associated Functions
Multiple impl Blocks
Summary

Chapter 6: Enums and Pattern Matching

Defining an Enum
Enum Values
The Option Enum and Its Advantages Over Null Values
The match Control Flow Construct
Patterns That Bind to Values
Matching with Option<T>
Matches Are Exhaustive
Catch-All Patterns and the _ Placeholder
Concise Control Flow with if let
Summary

Chapter 7: Managing Growing Projects with Packages, Crates, and Modules

Packages and Crates
Defining Modules to Control Scope and Privacy
Paths for Referring to an Item in the Module Tree
Exposing Paths with the pub Keyword
Starting Relative Paths with super
Making Structs and Enums Public
Bringing Paths into Scope with the use Keyword
Creating Idiomatic use Paths
Providing New Names with the as Keyword
Re-exporting Names with pub use
Using External Packages
Using Nested Paths to Clean Up Large use Lists
The Glob Operator
Separating Modules into Different Files
Summary

Chapter 8: Common Collections

Storing Lists of Values with Vectors
Creating a New Vector
Updating a Vector
Reading Elements of Vectors
Iterating Over the Values in a Vector
Using an Enum to Store Multiple Types
Dropping a Vector Drops Its Elements
Storing UTF-8 Encoded Text with Strings
What Is a String?
Creating a New String
Updating a String
Indexing into Strings
Slicing Strings
Methods for Iterating Over Strings
Strings Are Not So Simple
Storing Keys with Associated Values in Hash Maps
Creating a New Hash Map
Accessing Values in a Hash Map
Hash Maps and Ownership
Updating a Hash Map
Hashing Functions
Summary

Chapter 9: Error Handling

Unrecoverable Errors with panic!
Recoverable Errors with Result
Matching on Different Errors
Propagating Errors
To panic! or Not to panic!
Examples, Prototype Code, and Tests
Cases in Which You Have More Information Than the Compiler
Guidelines for Error Handling
Creating Custom Types for Validation
Summary

Chapter 10: Generic Types, Traits, and Lifetimes

Removing Duplication by Extracting a Function
Generic Data Types
In Function Definitions
In Struct Definitions
In Enum Definitions
In Method Definitions
Performance of Code Using Generics
Traits: Defining Shared Behavior
Defining a Trait
Implementing a Trait on a Type
Default Implementations
Traits as Parameters
Returning Types That Implement Traits
Using Trait Bounds to Conditionally Implement Methods
Validating References with Lifetimes
Preventing Dangling References with Lifetimes
The Borrow Checker
Generic Lifetimes in Functions
Lifetime Annotation Syntax
Lifetime Annotations in Function Signatures
Thinking in Terms of Lifetimes
Lifetime Annotations in Struct Definitions
Lifetime Elision
Lifetime Annotations in Method Definitions
The Static Lifetime
Generic Type Parameters, Trait Bounds, and Lifetimes Together
Summary

Chapter 11: Writing Automated Tests

How to Write Tests
The Anatomy of a Test Function
Checking Results with the assert! Macro
Testing Equality with the assert_eq! and assert_ne! Macros
Adding Custom Failure Messages
Checking for Panics with should_panic
Using Result<T, E> in Tests
Controlling How Tests Are Run
Running Tests in Parallel or Consecutively
Showing Function Output
Running a Subset of Tests by Name
Ignoring Some Tests Unless Specifically Requested
Test Organization
Unit Tests
Integration Tests
Summary

Chapter 12: An I/O Project: Building a Command Line Program

Accepting Command Line Arguments
Reading the Argument Values
Saving the Argument Values in Variables
Reading a File
Refactoring to Improve Modularity and Error Handling
Separation of Concerns for Binary Projects
Fixing the Error Handling
Extracting Logic from main
Splitting Code into a Library Crate
Developing the Library’s Functionality with Test-Driven Development
Writing a Failing Test
Writing Code to Pass the Test
Working with Environment Variables
Writing a Failing Test for the Case-Insensitive Search Function
Implementing the search_case_insensitive Function
Writing Error Messages to Standard Error Instead of Standard Output
Checking Where Errors Are Written
Printing Errors to Standard Error
Summary

With over 50,000 copies sold, The Rust Programming Language is the quintessential guide to programming in Rust. Thoroughly updated to Rust’s latest version, this edition is considered the language’s official documentation.
The Rust Programming Language "covers everything you could want to know about the language."—Stack Overflow
Rust has been repeatedly voted "Most Loved Language" on the StackOverflow Developer Survey.
The Rust Programming Language, 2nd Edition is the official guide to Rust 2021: an open source systems programming language that will help you write faster, more reliable software. Rust provides control of low-level details along with high-level ergonomics, allowing you to improve productivity and eliminate the hassle traditionally associated with low-level languages.
Klabnik and Nichols, alumni of the Rust Core Team, share their knowledge to help you get the most out of Rust’s features so that you can create robust and scalable programs. You’ll begin with basics like creating functions, choosing data types, and binding variables, then move on to more advanced concepts, such as:

  • Ownership and borrowing, lifetimes, generics, traits, and trait objects to communicate your program’s constraints to the compiler

  • Smart pointers and multithreading, and how ownership interacts with them to enable fearless concurrency

  • How to use Cargo, Rust’s built-in package manager, to build, document your code, and manage dependencies

  • The best ways to test, handle errors, refactor, and take advantage of expressive pattern matching

In addition to the countless code examples, you’ll find three chapters dedicated to building complete projects: a number-guessing game, a Rust implementation of a command line tool, and a multithreaded server.


Похожее:

Список отзывов:

Нет отзывов к книге.