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

Mastering Rust

Book Description

Discover the powerful, hidden features of Rust you need to build robust, concurrent, and fast applications

About This Book

  • Learn how concurrency works in Rust and why it is safe
  • Get to know the different philosophies of error handling and how to use them wisely
  • After reading this book, you will be able to migrate your legacy C or C++ application to a Rust environment

Who This Book Is For

The target audience would be readers having knowledge of other programming languages and are able to work fluently in the operating system of their choice, be it Linux, OS X or Windows. Since Rust is a rather new language, they are interested in programming beyond simply using it for work. The book focuses on intermediate and advanced features of Rust.

What You Will Learn

  • Implement unit testing patterns with the standard Rust tools
  • Get to know the different philosophies of error handling and how to use them wisely
  • Appreciate Rust's ability to solve memory allocation problems safely without garbage collection
  • Get to know how concurrency works in Rust and use concurrency primitives such as threads and message passing
  • Use syntax extensions and write your own
  • Create a Web application with Rocket
  • Use Diesel to build safe database abstractions

In Detail

If concurrent programs are giving you sleepless nights, Rust is your go-to language. Being one of the first ever comprehensive books on Rust, it is filled with real-world examples and explanations, showing you how you can build scalable and reliable programs for your organization.

We’ll teach you intermediate to advanced level concepts that make Rust a great language. Improving performance, using generics, building macros, and working with threads are just some of the topics we’ll cover. We’ll talk about the official toolsets and ways to discover more. The book contains a mix of theory interspersed with hands-on tasks, so you acquire the skills as well as the knowledge. Since programming cannot be learned by just reading, we provide exercises (and solutions) to hammer the concepts in.

After reading this book, you will be able to implement Rust for your enterprise project, deploy the software, and will know the best practices of coding in Rust.

Style and approach

This book is your one stop guide to the Rust programming language and covers advanced-level concepts in a detailed manner using real-world examples.

Downloading the example code for this book. You can download the example code files for all Packt books you have purchased from your account at http://www.PacktPub.com. If you purchased this book elsewhere, you can visit http://www.PacktPub.com/support and register to have the code file.

Table of Contents

  1. Preface
    1. What this book covers
    2. What you need for this book
    3. Who this book is for
    4. Conventions
    5. Reader feedback
    6. Customer support
      1. Downloading the example code
      2. Errata
      3. Piracy
      4. Questions
  2. Getting Your Feet Wet
    1. What is Rust and why should you care?
    2. Installing Rust compiler and Cargo
      1. Using rustup.rs
    3. A tour of the language and trying it out
      1. Constants and variables
      2. Loops
      3. Compound data
      4. Enums and pattern matching
    4. Struct methods
      1. Using other pieces of code in your module
      2. Sequences
      3. Exercise - fix the word counter
    5. Summary
  3. Using Cargo to Build Your First Program
    1. Cargo and crates
    2. Founding a project - cargo init
    3. Dependencies, building, and running
    4. Running tests - cargo test
    5. Cargo.toml - project metadata
    6. Editor integrations
    7. Final exercise - starting our project
    8. Summary
  4. Unit Testing and Benchmarking
    1. Motivation and high-level view
      1. Annotations
      2. Assert macros
      3. Integration or black box tests
      4. Documentation tests
      5. Benchmarks
      6. Integrating with Travis
    2. Founding a city-builder game
      1. Final exercise - fixing the tests
    3. Summary
  5. Types
    1. String types
      1. String slices
      2. The String type
      3. Byte strings
      4. Takeaways and tasks
    2. Arrays and slices
      1. Takeaways and tasks
    3. Generic types
      1. Takeaways and tasks
    4. Traits and implementations
      1. Takeaways and tasks
    5. Constants and statics
    6. Summary
  6. Error Handling
    1. Option and Result
    2. Unwrapping
    3. Mapping of the Option/Result values
    4. Early returns and the try! macro
      1. The ? operator
    5. Panicking
    6. Custom errors and the Error trait
    7. Exercise
    8. Summary
  7. Memory, Lifetimes, and Borrowing
    1. LLVM
    2. Function variables and the stack
    3. The heap
    4. Memory safety
    5. Ownership
      1. Copy trait
      2. Function parameters and patterns
      3. Borrowing
    6. Lifetimes
      1. Globals
      2. References as function parameters
      3. Structs and struct fields
      4. Impl signatures
    7. The Drop trait
    8. Collector types
      1. Box<T>
      2. Interior mutability for Copy types - Cell<T>
      3. Interior mutability for move types - RefCell<T>
      4. Practical uses of interior mutability
      5. Reference collected memory: Rc<T> and Arc<T>
    9. Inspecting memory usage with std::mem
    10. Final exercises
    11. Summary
  8. Concurrency
    1. Problems with concurrency
    2. Closures
      1. Exercises
    3. Threads
      1. Exercises
    4. Sharing the Copy types
    5. Channels
      1. Exercises
    6. Locks and mutexes
      1. Exercises
    7. Atomic Rc
      1. Exercises
    8. The final exercise
    9. Summary
  9. Macros
    1. Introduction to metaprogramming
    2. Dissecting println!
      1. Exercises
    3. Debugging macros
    4. Macro keywords
      1. block
      2. expr
      3. ident
      4. item
      5. meta
      6. pat
      7. path
      8. stmt
      9. tt
      10. ty
    5. Repeating constructs
    6. Example - an HTTP tester
    7. Exercises
    8. Summary
  10. Compiler Plugins
    1. Basics of compiler plugins
      1. The minimal compiler plugin
      2. Building a compiler plugin via Cargo
    2. Code generation as a workaround
    3. Aster
    4. Linter plugins
    5. Macros 1.1 - custom derives
    6. Exercises
    7. Summary
  11. Unsafety and Interfacing with Other Languages
    1. Unsafety
    2. Calling C code from Rust
    3. Connecting external libraries to Rust code
    4. Creating Ruby extensions with Ruru
    5. JavaScript/Node.js and Neon
    6. Exercises
    7. Summary
  12. Parsing and Serialization
    1. Parsing fundamentals
      1. nom
      2. Chomp
    2. Other parser libraries
      1. Serde
    3. Exercises
    4. Summary
  13. Web Programming
    1. Introduction to Rust and web programming
    2. Hyper as a client
    3. Hyper as a server
    4. Rocket
    5. Other web frameworks
    6. Exercises
    7. Summary
  14. Data Storage
    1. SQLite
    2. PostgreSQL
    3. Connection pooling with r2d2
    4. Diesel
    5. Summary
  15. Debugging
    1. Introduction to debugging
      1. GDB - basics
      2. GDB - threads
      3. LLDB - quick overview
      4. Editor integration with Visual Studio Code
    2. Exercises
    3. Summary
  16. Solutions and Final Words
    1. Chapter 1 - Getting Your Feet Wet
      1. Exercise - fix the word counter
    2. Chapter 2 - Using Cargo to Build Your First Program
      1. Exercise - starting our project
    3. Chapter 3 - Unit Testing and Benchmarking
      1. Exercise - fixing the tests
    4. Chapter 4 - Types
      1. Exercise - various throughout the chapter
    5. Chapter 5 - Error Handling
      1. Exercise solutions
    6. Chapter 6 - Memory, Lifetimes, and Borrowing
      1. Exercises
    7. Chapter 7 - Concurrency
      1. Exercises
    8. Chapter 8 - Macros
      1. Exercises
    9. Chapter 9 - Compiler Plugins
      1. Exercises
    10. Chapter 10 - Unsafety and Interfacing with Other Languages
      1. Exercises
    11. Chapter 11 - Parsing and Serialization
      1. Exercises
    12. Chapter 12 - Web Programming
      1. Exercises
    13. Chapter 13 - Data Storage
    14. Chapter 14 - Debugging
      1. Exercises