- 1. Rust for C programmers
- 1.1. Why Rust
- 1.2. What makes Rust special
- 1.3. About the book
- 1.4. About the authors
- 2. The basic structure of a Rust program
- 2.1. The compilation process
- 2.2. The program structure
- 2.3. The main() function
- 2.4. Variables and mutability
- 2.5. Data types
- 2.6. Constants and statics
- 2.7. Functions
- 2.8. Control flow
- 2.9. Modules and crates
- 2.10. Use statement and namespacing
- 2.11. Traits
- 2.12. Macros
- 2.13. Error handling
- 2.14. Memory safety and ownership
- 2.15. Expressions and statements
- 2.16. Code conventions and style
- 2.17. Comments and documentation
- 2.18. Additional topics
- 2.19. Summary
- 3. Installing Rust
- 3.1. Rustup
- 3.2. Using system package managers
- 3.3. Experimenting in the playground
- 3.4. IDE and editor support
- 3.5. Summary
- 4. Rustc and Cargo
- 4.1. Compiling with Rustc
- 4.2. Introduction to Cargo
- 4.3. Summary
- 5. Common programming concepts
- 5.1. Keywords
- 5.2. Identifiers and allowed characters
- 5.3. Expressions and statements
- 5.4. Data types
- 5.5. Variables and mutability
- 5.6. Operators
- 5.7. Numeric literals and their type
- 5.8. Overflow in arithmetic operations
- 5.9. Performance for numeric types
- 5.10. Comments in Rust
- 5.11. Summary
- 6. Ownership and memory management
- 6.1. Overview of ownership
- 6.2. Moves, cloning, and copying
- 6.3. Borrowing and references
- 6.4. The String type
- 6.5. Slices -- borrowing subranges
- 6.6. Lifetimes of references
- 6.7. Smart pointers
- 6.8. Unsafe Rust
- 6.9. Comparison with C
- 6.10. Summary
- 7. Control flow
- 7.1. Conditional statements
- 7.2. Match statement
- 7.3. Loops
- 7.4. Summary
- 8. Functions and methods
- 8.1. The main function
- 8.2. Defining and calling functions
- 8.3. Function parameter types
- 8.4. Returning values
- 8.5. Nested functions and scope
- 8.6. Default and named arguments
- 8.7. Slices and Tuples as parameters
- 8.8. Generic functions
- 8.9. Function pointers
- 8.10. Recursion
- 8.11. Inlining functions
- 8.12. Method call syntax
- 8.13. Function overloading
- 8.14. Type inference for return types
- 8.15. Variadic functions and macros
- 8.16. Summary
- 8.17. Exercises
- 9. Structs
- 9.1. Introduction
- 9.2. Defining and instantiating
- 9.3. Updating struct instances
- 9.4. Default values
- 9.5. Tuple and unit like structs
- 9.6. Methods, associated functions
- 9.7. Getters and setters
- 9.8. Structs and ownership
- 9.9. Generic structs
- 9.10. Derived traits
- 9.11. Memory layout
- 9.12. Visibility
- 9.13. Summary
- 9.14. Exercises
- 10. Enums and pattern matching
- 10.1. Understanding enums
- 10.2. Basic enums
- 10.3. Enums with data
- 10.4. Using enums in code
- 10.5. Memory layout of enums
- 10.6. Enums vs inheritance in OOP
- 10.7. Limitations of enums
- 10.8. Common use cases
- 10.9. Option and Result types
- 10.10. Summary
- 11. Traits, generics, and lifetimes
- 11.1. Understanding traits
- 11.2. Generics in Rust
- 11.3. Lifetimes in Rust
- 11.4. Further trait features
- 11.5. Summary
- 12. Closures in Rust
- 12.1. Introduction to closures
- 12.2. Closure traits
- 12.3. Using closures
- 12.4. Performance considerations
- 12.5. Advanced_topics
- 12.6. Summary
- 13. Working with Iterators
- 13.1. Introduction to iterators
- 13.2. Common iterator methods
- 13.3. Creating custom iterators
- 13.4. Advanced iterator concepts
- 13.5. Performance considerations
- 13.6. Practical examples
- 13.7. Iterators for complex structures
- 13.8. Summary
- 14. Option types
- 14.1. Introduction to option types
- 14.2. Using option types
- 14.3. Performance considerations
- 14.4. Best practices
- 14.5. Practical examples
- 14.6. Summary
- 15. Error handling with Result
- 15.1. Recoverable vs. unrec. errors
- 15.2. The Result type
- 15.3. Error propagation with ?
- 15.4. Handling multiple error types
- 15.5. Unrecoverable errors
- 15.6. Best practices
- 15.7. Summary
- 16. Type conversions
- 16.1. Explicit and safe conversions
- 16.2. Casting with as
- 16.3. Using the From and Into traits
- 16.4. Fallible conversions
- 16.5. Reinterpreting data types
- 16.6. String processing and parsing
- 16.7. Best practices
- 16.8. Summary
- 17. Crates, modules, and packages
- 17.1. Packages
- 17.2. Crates
- 17.3. Modules
- 17.4. Best practices
- 17.5. Summary
- 18. Common collection types
- 18.1. Overview
- 18.2. Vectors
- 18.3. Strings
- 18.4. HashMaps
- 18.5. Other collection
- 18.6. Performance considerations
- 18.7. Selection guide
- 18.8. Summary
- 19. Smart pointers
- 19.1. The concept of smart pointers
- 19.2. Smart pointers vs. references
- 19.3. Comparing to C and C++
- 19.4. Box -- a simple smart pointer
- 19.5. Rc -- shared_ownership
- 19.6. Interior mutability
- 19.7. Shared ownership and threads
- 19.8. Weak -- non owning references
- 19.9. Summary
- 20. Object oriented programming
- 20.1. Definition of OOP
- 20.2. Problems and criticisms
- 20.3. OOP in Rust
- 20.4. Trait objects
- 20.5. Disadvantages of trait objects
- 20.6. Trait objects vs. enums
- 20.7. Modules and encapsulation
- 20.8. Generics instead of OOP
- 20.9. Serializing trait objects
- 20.10. Summary
- 21. Patterns and pattern matching
- 21.1. C's switch statement
- 21.2. Overview of patterns
- 21.3. Refutable vs irrefutable patterns
- 21.4. Variable assignment as a pattern
- 21.5. Match expressions
- 21.6. Matching enums
- 21.7. Matching literals and ranges
- 21.8. Underscores and the .. pattern
- 21.9. Variable bindings with @
- 21.10. Match guards
- 21.11. Destructuring complex types
- 21.12. Matching boxed types
- 21.13. If let and while let
- 21.14. The let else construct
- 21.15. If let chains
- 21.16. Patterns in for loops
- 21.17. Nested patterns
- 21.18. Partial moves
- 21.19. Performance considerations
- 21.20. Summary
- 22. Fearless concurrency
- 22.1. Concurrency, processes, threads
- 22.2. Concurrency vs true parallelism
- 22.3. Threads vs async
- 22.4. Creating threads
- 22.5. Sharing data between threads
- 22.6. Channels for message passing
- 22.7. Rayon for data parallelism
- 22.8. SIMD
- 22.9. Comparison to C++
- 22.10. Send and sync traits
- 22.11. Summary
- 23. Working with Cargo
- 23.1. Overview
- 23.2. Cargo's command line interface
- 23.3. Directory structure
- 23.4. Cargo TOML file
- 23.5. Building and running projects
- 23.6. Build profiles
- 23.7. Testing and benchmarking
- 23.8. Creating documentation
- 23.9. Publishing a crate to crates.io
- 23.10. Binary vs library crates
- 23.11. Cargo workspaces
- 23.12. Installing application packages
- 23.13. Security considerations
- 23.14. Summary
- 24. Testing
- 24.1. Overview
- 24.2. Writing basic tests
- 24.3. Test organization
- 24.4. Controlling test execution
- 24.5. Testing panics and errors
- 24.6. Documentation tests
- 24.7. Development dependencies
- 24.8. Benchmarking
- 24.9. Profiling
- 24.10. Summary
- 25. Unsafe Rust
- 25.1. Overview
- 25.2. Unsafe blocks and functions
- 25.3. Raw pointers
- 25.4. Calling C functions (FFI)
- 25.5. Mutable global variables
- 25.6. Unsafe traits
- 25.7. Unions
- 25.8. Advanced unsafe operations
- 25.9. Verifying unsafe code
- 25.10. Summary
26. Macros
27. Async
- 28. Private policy