Programming with Rust 9780137889396, 9780137889655, 0137889658

Make Rust a key tool in your arsenal, and access one of the industry's fastest-growing areas of opportunity. Rust&

143 123 22MB

English Pages 400 Year 2024

Report DMCA / Copyright

DOWNLOAD FILE

Programming with Rust
 9780137889396, 9780137889655, 0137889658

Table of contents :
Cover Page
About This eBook
Halftitle Page
Title Page
Copyright Page
Pearson’s Commitment to Diversity, Equity, and Inclusion
Contents
About the Author
1. Introduction to Rust
Introduction
Features
Rust Terminology
Tools
Summary
2. Getting Started
Preliminaries
Rust and Windows
Installing Rust
Advanced Rustup
“Hello, World”
Compile and Run
Cargo
Library
Comments
Published Crates
Main Function
Command-Line Arguments
Summary
3. Variables
Terminology
Variables
Primitives
Integer Types
Overflow
Notations
Floating Point Types
Floating Point Constants
Infinity
NaN
Numeric Ranges
Casting
Boolean Types
Char
Pointers
Operators
Summary
4. Strings
Str
String
Summary
5. Console
Print
Positional Arguments
Variable Arguments
Named Arguments
Padding, Alignment, and Precision
Base
Developer Facing
Write! Macro
Display Trait
Debug Trait
Format! Macro
Console Read and Write
Summary
6. Control Flow
The if Expression
The while Expression
The for Expression
The loop Expression
The loop Label
The Iterator Trait
Summary
7. Collections
Arrays
Vectors
HashMap
Summary
8. Ownership
Stack and Heap Memory
Shallow versus Deep Copy
Car Analogy
Move Semantics
Borrow
Copy Semantics
Clone Trait
Copy Trait
Clone Trait
Summary
9. Lifetimes
Introduction to Lifetimes
Function Headers and Lifetimes
Lifetime Annotation
Lifetime Elision
Complex Lifetimes
Sharing a Lifetime
Static Lifetimes
Structs and Lifetimes
Methods and Lifetimes
Subtyping Lifetimes
Anonymous Lifetimes
Generics and Lifetimes
Summary
10. References
Declaration
Borrowing
Dereferencing
Comparing References
Reference Notation
Reference to Reference
Mutability
Limits to Multiple Borrowers
Summary
11. Functions
Function Definition
Parameters
Function Return
Const Functions
Nested Functions
Function Pointers
Function Aliases
Summary
12. Error Handling
Handling Error Handling
The Result Enum
The Option Enum
Panics
Unwrapping
Match Pattern for Result and Option
Map
Rich Errors
Custom Errors
Summary
13. Structures
Alternate Initialization
Move Semantics
Mutability
Methods
Self
Associated Functions
Impl Blocks
Operator Overloading
Tuple Struct
Summary
14. Generics
Generic Functions
Bounds
The where Clause
Structs
Associated Functions
Enums
Generic Traits
Explicit Specialization
Summary
15. Patterns
Let Statement
Wildcards
Complex Patterns
Ownership
Irrefutable
Ranges
Multiple Patterns
Control Flow
Structs
Functions
Match Expressions
Match Guards
Summary
16. Closures
“Hello, World”
Closure Syntax
Closed Over
Closures as Function Arguments
Closures as Function Return Values
Implementation of Closures
Matrix Example
Summary
17. Traits
Trait Definition
Default Functions
Marker Trait
Associated Functions
Associated Types
Extension Methods
Fully Qualified Syntax
Supertraits
Static Dispatch
Dynamic Dispatch
Enums and Traits
Summary
18. Threads 1
Synchronous Function Calls
Threads
The Thread Type
Processor Time
Builder
Communicating Sequential Process
Asynchronous Channel
Synchronous Channel
Rendezvous Channel
The try Methods
Store Example
Summary
19. Threads 2
Mutex
Nonscoped Mutex
Mutex Poisoning
Reader-Writer Lock
Condition Variables
Atomic Operations
Summary
20. Memory
Stacks
Static Values
The Heap
Interior Mutability
RefCell
OnceCell
Summary
21. Macros
Tokens
Declarative Macros
Procedural Macros
Summary
22. Interoperability
Foreign Function Interface
Basic Example
Libc Crate
Structs
Bindgen
C Calling Rust Functions
Cbindgen
Summary
23. Modules
Module Items
Functions and Modules
The crate, super, and self Keywords
Legacy Model
Summary
Index
Code Snippets

Polecaj historie