You are previewing Rust Essentials.
O'Reilly logo
Rust Essentials

Book Description

Discover how to use Rust to write fast, secure, and concurrent systems and applications

In Detail

Starting by comparing Rust with other programming languages, this book will show you where and how to use Rust. It will discuss primitive types along with variables and their scope, binding and casting, simple functions, and ways to control execution flow in a program.

Next, the book covers flexible arrays, vectors, tuples, enums, and structs. You will then generalize the code with higher-order functions and generics applying it to closures, iterators, consumers, and so on. Memory safety is ensured by the compiler by using references, pointers, boxes, reference counting, and atomic reference counting. You will learn how to build macros and crates and discover concurrency for multicore execution.

By the end of this book, you will have successfully migrated to using Rust and will be able to use it as your main programming language.

What You Will Learn

  • Set up your Rust environment for maximum productivity

  • Bridge the performance gap between safe and unsafe languages with Rust

  • 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 threads to tackle problems concurrently and in distributed environments

  • Interface with C and isolate unsafe code

  • 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 files e-mailed directly to you.

    Table of Contents

    1. Rust Essentials
      1. Table of Contents
      2. Rust Essentials
      3. Credits
      4. About the Author
      5. About the Reviewers
      6. www.PacktPub.com
        1. Support files, eBooks, discount offers, and more
          1. Why subscribe?
          2. Free access for Packt account holders
      7. 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
      8. 1. Starting with Rust
        1. The advantages of Rust
        2. The trifecta of Rust – safety, speed, and concurrency
          1. Comparison with other languages
        3. Using Rust
          1. Servo
        4. Installing Rust
        5. The Rust compiler – rustc
        6. Our first program
        7. Working with Cargo
        8. The developer tools
          1. Using Sublime Text
          2. Other tools
        9. Summary
      9. 2. Using Variables and Types
        1. Comments
        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
      10. 3. 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
        6. Summary
      11. 4. 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. Summary
      12. 5. Generalizing Code with Higher-order Functions and Parametrization
        1. Higher-order functions and closures
        2. Iterators
        3. Consumers and adapters
        4. Generic data structures and functions
        5. Error handling
          1. Panics
          2. Failures
        6. Methods on structs
        7. Traits
        8. Using trait constraints
        9. Built-in traits and operator overloading
        10. Summary
      13. 6. Pointers and Memory Safety
        1. Pointers and references
          1. The stack and the heap
            1. Lifetimes
            2. Copying values and the Copy trait
            3. Pointers
            4. References
            5. Using ref in a match
        2. Ownership and borrowing
        3. Boxes
        4. Reference counting
        5. An overview of pointers
        6. Summary
      14. 7. Organizing Code and Macros
        1. Modules and crates
          1. Building crates
          2. Defining a module
          3. The 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. The test module
        2. Macros
          1. Why do we use macros?
          2. Developing macros
            1. Repetition
            2. Creating a new function
            3. Using macros from crates
        3. Summary
      15. 8. Concurrency and Parallelism
        1. Concurrency and threads
          1. Creating threads
          2. Starting a number of threads
          3. Panicking threads
          4. Thread-safety
        2. The shared mutable state
          1. The Sync trait
        3. Communication through channels
          1. Sending and receiving data
          2. Synchronous and asynchronous communication
        4. Summary
      16. 9. Programming at the Boundaries
        1. Program arguments
        2. Unsafe code
        3. Raw pointers
        4. Interfacing with C
          1. Using a C library
          2. Inlining assembly code
        5. Calling Rust from other languages
        6. Summary
      17. A. Exploring Further
        1. Stability of Rust and the standard library
        2. The ecosystem of crates
        3. Other resources for learning Rust
          1. Files and databases
          2. Graphics and games
          3. Web development
      18. Index