Rust Essentials - Second Edition

Book description

Leverage the functional programming and concurrency features of Rust and speed up your application development

About This Book

  • Get started with Rust to build scalable and high performance applications
  • Enhance your application development skills using the power of Rust
  • Discover the power of Rust when developing concurrent applications for large and scalable software

Who This Book Is For

The book is for developers looking for a quick entry into using Rust and understanding the core features of the language. Basic programming knowledge is assumed.

What You Will Learn

  • Set up your Rust environment to achieve the highest productivity
  • Bridge the performance gap between safe and unsafe languages
  • Use pattern matching to create flexible code
  • Apply generics and traits to develop widely applicable code
  • Organize your code in modules and crates
  • Build macros to extend Rust's capabilities and reach
  • Apply tasks to tackle problems concurrently in a distributed environment

In Detail

Rust is the new, open source, fast, and safe systems programming language for the 21st century, developed at Mozilla Research, and with a steadily growing community. It was created to solve the dilemma between high-level, slow code with minimal control over the system, and low-level, fast code with maximum system control. It is no longer necessary to learn C/C++ to develop resource intensive and low-level systems applications. This book will give you a head start to solve systems programming and application tasks with Rust.

We start off with an argumentation of Rust's unique place in today's landscape of programming languages. You'll install Rust and learn how to work with its package manager Cargo. The various concepts are introduced step by step: variables, types, functions, and control structures to lay the groundwork. Then we explore more structured data such as strings, arrays, and enums, and you'll see how pattern matching works.

Throughout all this, we stress the unique ways of reasoning that the Rust compiler uses to produce safe code. Next we look at Rust's specific way of error handling, and the overall importance of traits in Rust code. The pillar of memory safety is treated in depth as we explore the various pointer kinds. Next, you'll see how macros can simplify code generation, and how to compose bigger projects with modules and crates. Finally, you'll discover how we can write safe concurrent code in Rust and interface with C programs, get a view of the Rust ecosystem, and explore the use of the standard library.

Style and approach

The book takes a pragmatic approach, showing various methods to solve systems programming tasks with Rust and develop resource intensive and low-level systems applications.

Publisher resources

Download Example Code

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. Customer support
      1. Downloading the example code
      2. Errata
      3. Piracy
      4. Questions
  2. Starting with Rust
    1. The advantages of Rust
      1. The trifecta of Rust - safe, fast, and concurrent
      2. Comparison with other languages
    2. The stability of Rust and its evolution
    3. The success of Rust
      1. Where to use Rust
    4. Servo
    5. Installing Rust
    6. rustc--the Rust compiler
    7. Our first program
      1. Working with Cargo
    8. Developer tools
      1. Using Sublime Text
    9. The Standard Library
    10. Summary
  3. Using Variables and Types
    2. Global constants
      1. Printing with string interpolation
    3. Values and primitive types
      1. Consulting Rust documentation
    4. Binding variables to values
      1. Mutable and immutable variables
    5. Scope of a variable and shadowing
    6. Type checking and conversions
      1. Aliasing
    7. Expressions
    8. The stack and the heap
    9. Summary
  4. Using Functions and Control Structures
    1. Branching on a condition
    2. Looping
    3. Functions
      1. Documenting a function
    4. Attributes
      1. Conditional compilation
    5. Testing
      1. Testing with cargo
        1. The tests module
    6. Summary
  5. Structuring Data and Matching Patterns
    1. Strings
    2. Arrays, vectors, and slices
      1. Vectors
      2. Slices
      3. Strings and arrays
    3. Tuples
    4. Structs
    5. Enums
      1. Result and Option
    6. Getting input from the console
    7. Matching patterns
    8. Program arguments
    9. Summary
  6. Higher Order Functions and Error-Handling
    1. Higher order functions and closures
    2. Iterators
    3. Consumers and adapters
    4. Generic data structures and functions
    5. Error-handling
      1. Panics
      2. Testing for failure
      3. Some more examples of error-handling
      4. The try! macro and the ? operator
    6. Summary
  7. Using Traits and OOP in Rust
    1. Associated functions on structs
    2. Methods on structs
    3. Using a constructor pattern
    4. Using a builder pattern
    5. Methods on tuples and enums
    6. Traits
    7. Using trait constraints
    8. Static and dynamic dispatch
    9. Built-in traits and operator overloading
    10. OOP in Rust
    11. Inheritance with traits
    12. Using the visitor pattern
    13. Summary
  8. Ensuring Memory Safety and Pointers
    1. Pointers and references
      1. Stack and heap
      2. Lifetimes
      3. Copying and moving values - The copy trait
        1. Let's summarize
      4. Pointers
      5. References
        1. Match, struct, and ref
    2. Ownership and borrowing
      1. Ownership
      2. Moving a value
      3. Borrowing a value
      4. Implementing the Drop trait
      5. Moving closure
    3. Boxes
    4. Reference counting
    5. Overview of pointers
    6. Summary
  9. Organizing Code and Macros
    1. Modules and crates
      1. Building crates
      2. Defining a module
      3. Visibility of items
      4. Importing modules and file hierarchy
      5. Importing external crates
      6. Exporting a public interface
      7. Adding external crates to a project
      8. Working with random numbers
    2. Macros
      1. Why macros?
      2. Developing macros
        1. Repetition
        2. Creating a new function
        3. Some other examples
      3. Using macros from crates
      4. Some other built-in macros
    3. Summary
  10. Concurrency - Coding for Multicore Execution
    1. Concurrency and threads
      1. Creating threads
        1. Setting the thread's stack size
      2. Starting a number of threads
      3. Panicking threads
      4. Thread safety
    2. Shared mutable states
      1. The Sync trait
    3. Communication through channels
      1. Sending and receiving data
        1. Making a channel
        2. Sending struct values over a channel
        3. Sending references over a channel
        4. Synchronous and asynchronous
    4. Summary
  11. Programming at the Boundaries
    1. When is code unsafe
      1. Using std::mem
    2. Raw pointers
    3. Interfacing with C
      1. Using a C library
    4. Inlining assembly code
    5. Calling Rust from other languages
    6. Summary
  12. Exploring the Standard Library
    1. Exploring std and the prelude module
    2. Collections - using hashmaps and hashsets
    3. Working with files
      1. Paths
      2. Reading a file
      3. Error-handling with try!
      4. Buffered reading
      5. Writing a file
      6. Error-handling with try!
      7. Filesystem operations
    4. Using Rust without the Standard Library
    5. Summary
  13. The Ecosystem of Crates
    1. The ecosystem of crates
    2. Working with dates and times
    3. File formats and databases
    4. Web development
    5. Graphics and games
    6. OS and embedded system development
    7. Other resources for learning Rust
    8. Summary

Product information

  • Title: Rust Essentials - Second Edition
  • Author(s): Ivo Balbaert
  • Release date: November 2017
  • Publisher(s): Packt Publishing
  • ISBN: 9781788390019