Effective Rust
35 Specific Ways to Improve Your Rust Code
Paperback Engels 2024 1e druk 9781098151409Samenvatting
Rust's popularity is growing, due in part to features like memory safety, type safety, and thread safety. But these same elements can also make learning Rust a challenge, even for experienced programmers. This practical guide helps you make the transition to writing idiomatic Rust—while also making full use of Rust's type system, safety guarantees, and burgeoning ecosystem.
If you're a software engineer who has experience with an existing compiled language, or if you've struggled to convert a basic understanding of Rust syntax into working programs, this book is for you. By focusing on the conceptual differences between Rust and other compiled languages, and by providing specific recommendations that programmers can easily follow, Effective Rust will soon have you writing fluent Rust, not just badly translated C++.
- Understand the structure of Rust's type system
- Learn Rust idioms for error handling, iteration, and more
- Discover how to work with Rust's crate ecosystem
- Use Rust's type system to express your design
- Win fights with the borrow checker
- Build a robust project that takes full advantage of the Rust tooling ecosystem
Specificaties
Lezersrecensies
Inhoudsopgave
Who This Book Is For
Rust Version
Navigating This Book
Conventions Used in This Book
O’Reilly Online Learning
How to Contact Us
Acknowledgments
1. Types
Item 1: Use the type system to express your data structures
Fundamental Types
Aggregate Types
enums
enums with Fields
Ubiquitous enum Types
Item 2: Use the type system to express common behavior
Functions and Methods
Function Pointers
Closures
Traits
Item 3: Prefer Option and Result transforms over explicit match expressions
Things to Remember
Item 4: Prefer idiomatic Error types
The Error Trait
Minimal Errors
Nested Errors
Trait Objects
Libraries Versus Applications
Things to Remember
Item 5: Understand type conversions
User-Defined Type Conversions
Casts
Coercion
Item 6: Embrace the newtype pattern
Bypassing the Orphan Rule for Traits
Newtype Limitations
Item 7: Use builders for complex types
Item 8: Familiarize yourself with reference and pointer types
Rust References
Pointer Traits
Fat Pointer Types
More Pointer Traits
Smart Pointer Types
Item 9: Consider using iterator transforms instead of explicit loops
Iterator Traits
Iterator Transforms
Iterator Consumers
Building Collections from Result Values
Loop Transformation
When Explicit Is Better
2. Traits
Item 10: Familiarize yourself with standard traits
Common Standard Traits
Standard Traits Covered Elsewhere
Operator Overloads
Summary
Item 11: Implement the Drop trait for RAII patterns
Item 12: Understand the trade-offs between generics and trait objects
Generics
Trait Objects
Basic Comparisons
More Trait Bounds
Trait Object Safety
Trade-Offs
Item 13: Use default implementations to minimize required trait methods
3. Concepts
Item 14: Understand lifetimes
Introduction to the Stack
Evolution of Lifetimes
Scope of a Lifetime
Algebra of Lifetimes
Lifetime Elision Rules
The 'static Lifetime
Lifetimes and the Heap
Lifetimes in Data Structures
Anonymous Lifetimes
Things to Remember
Item 15: Understand the borrow checker
Access Control
Borrow Rules
Owner Operations
Winning Fights Against the Borrow Checker
Things to Remember
Item 16: Avoid writing unsafe code
Item 17: Be wary of shared-state parallelism
Data Races
Deadlocks
Advice
Item 18: Don’t panic
Item 19: Avoid reflection
Upcasting in Future Versions of Rust
Item 20: Avoid the temptation to over-optimize
Data Structures and Allocation
Who’s Afraid of the Big Bad Copy?
References and Smart Pointers
4. Dependencies
Item 21: Understand what semantic versioning promises
Semver Essentials
Semver for Crate Authors
Semver for Crate Users
Discussion
Item 22: Minimize visibility
Visibility Syntax
Visibility Semantics
Item 23: Avoid wildcard imports
Item 24: Re-export dependencies whose types appear in your API
Item 25: Manage your dependency graph
Version Specification
Solving Problems with Tooling
What to Depend On
Things to Remember
Item 26: Be wary of feature creep
Conditional Compilation
Features
Things to Remember
5. Tooling
Item 27: Document public interfaces
Tooling
Additional Documentation Locations
Published Crate Documentation
What Not to Document
Things to Remember
Item 28: Use macros judiciously
Declarative Macros
Procedural Macros
When to Use Macros
Disadvantages of Macros
Advice
Item 29: Listen to Clippy
Item 30: Write more than unit tests
Unit Tests
Integration Tests
Doc Tests
Examples
Benchmarks
Fuzz Testing
Testing Advice
Things to Remember
Item 31: Take advantage of the tooling ecosystem
Tools to Remember
Item 32: Set up a continuous integration (CI) system
CI Steps
CI Principles
Public CI Systems
6. Beyond Standard Rust
Item 33: Consider making library code no_std compatible
core
alloc
Writing Code for no_std
Fallible Allocation
Things to Remember
Item 34: Control what crosses FFI boundaries
Invoking C Functions from Rust
Accessing C Data from Rust
Lifetimes
Invoking Rust from C
Things to Remember
Item 35: Prefer bindgen to manual FFI mappings
Beyond C
Afterword
Index
About the Author
Rubrieken
- advisering
- algemeen management
- coaching en trainen
- communicatie en media
- economie
- financieel management
- inkoop en logistiek
- internet en social media
- it-management / ict
- juridisch
- leiderschap
- marketing
- mens en maatschappij
- non-profit
- ondernemen
- organisatiekunde
- personal finance
- personeelsmanagement
- persoonlijke effectiviteit
- projectmanagement
- psychologie
- reclame en verkoop
- strategisch management
- verandermanagement
- werk en loopbaan