Rust Quick Start Guide

Book description

Get familiar with writing programs in the trending new systems programming language that brings together the powerful performance of low-level languages with the advanced features like thread safety in multi-threaded code

Key Features

  • Learn the semantics of Rust, which can be significantly different from other programming languages
  • Understand clearly how to work with the Rust compiler which strictly enforces rules that may not be obvious
  • Examples and insights beyond the Rust documentation

Book Description

Rust is an emerging programming language applicable to areas such as embedded programming, network programming, system programming, and web development. This book will take you from the basics of Rust to a point where your code compiles and does what you intend it to do!

This book starts with an introduction to Rust and how to get set for programming, including the rustup and cargo tools for managing a Rust installation and development workflow.

Then you'll learn about the fundamentals of structuring a Rust program, such as functions, mutability, data structures, implementing behavior for types, and many more. You will also learn about concepts that Rust handles differently from most other languages.

After understanding the Basics of Rust programming, you will learn about the core ideas, such as variable ownership, scope, lifetime, and borrowing. After these key ideas, you will explore making decisions in Rust based on data types by learning about match and if let expressions. After that, you'll work with different data types in Rust, and learn about memory management and smart pointers.

What you will learn

  • Install Rust and write your first program with it
  • Understand ownership in Rust
  • Handle different data types
  • Make decisions by pattern matching
  • Use smart pointers
  • Use generic types and type specialization
  • Write code that works with many data types
  • Tap into the standard library

Who this book is for

This book is for people who are new to Rust, either as their first programming language or coming to it from somewhere else. Familiarity with computer programming in any other language will be helpful in getting the best out of this book.

Publisher resources

Download Example Code

Table of contents

  1. Title Page
  2. Copyright and Credits
    1. Rust Quick Start Guide
  3. About Packt
    1. Why subscribe?
  4. Contributors
    1. About the author
    2. About the reviewers
    3. Packt is searching for authors like you
  5. Preface
    1. Who this book is for
    2. What this book covers
    3. To get the most out of this book
      1. Download the example code files
      2. Download the color images
      3. Conventions used
    4. Get in touch
      1. Reviews
  6. Getting Ready
    1. Installing Rust
    2. Starting a new project
      1. Project metadata
        1. Dependencies on libraries from
        2. Dependencies on Git repositories
        3. Dependencies on local libraries
      2. Automatically generated source files
    3. Compiling our project
      1. Debug and release builds
      2. Dynamic libraries, software distribution, and Rust
    4. Using
    5. Summary
  7. Basics of the Rust Language
    1. Functions
      1. Defining a function
    2. Modules
      1. Defining a module
        1. A module as a section of a file
        2. A module as a separate file
      2. Accessing module contents from outside
        1. Using the item's full name directly
        2. Using the item's short name
        3. Public and private module items
    3. Expressions
      1. Literal expressions
      2. Operator expressions
      3. Array and tuple expressions
      4. Block expressions
      5. Branch expressions
      6. Loop expressions
        1. while loops
        2. for loops
    4. Variables, types, and mutability
      1. Type inference
    5. Data structures
      1. Mutability of data structures
    6. More about functions
      1. Parameters
      2. Return types
      3. Error handling
        1. Using Result to signal success or failure
        2. Calling functions that return Result
    7. Implementing behavior for types
    8. Summary
  8. The Big Ideas – Ownership and Borrowing
    1. Scope and ownership
      1. The stack
    2. Transferring ownership
    3. Copying
    4. Lending
      1. Lending immutably
      2. Lending mutably
    5. Accessing borrowed data
    6. The lifetime of borrowed data
    7. Ownership and the self parameter
      1. Moving self
      2. Borrowing self
      3. Mutably borrowing self
    8. Summary
  9. Making Decisions by Pattern Matching
    1. Variable assignment with pattern matching
    2. Using if let expressions to test whether a pattern matches
    3. Using match to choose one of several patterns
    4. Using don't care in patterns
    5. Moving and borrowing in pattern matches
    6. Matching tuples and other more complex patterns
      1. Nested patterns
      2. Storing a matched value and comparing it to a pattern
      3. Ignoring most of a data structure
    7. Gotchas
      1. Not all values can be matched against a literal pattern
      2. Patterns assign values to variable names
    8. Summary
  10. One Data Type Representing Multiple Kinds of Data
    1. Enumerations
      1. Basic enumerations
      2. Parameterized enumerations
      3. Checking the value type and extracting parameter values
      4. Result is an enumeration, accessed via the prelude
    2. Traits and trait objects
      1. Traits
        1. Implementing our PrintableDirection trait
      2. Trait objects
        1. Using our PrintableDirection trait
        2. Trait objects only provide access to the trait interface
    3. Any
      1. Any can store almost anything
      2. But to access it we have to already know about the real data type
    4. Comparison of these techniques
    5. Summary
  11. Heap Memory and Smart Pointers
    1. Box
      1. Box and variable size
      2. Box and Any
    2. Vec and String
      1. String
      2. Vec
    3. Rc
      1. Weak references
    4. Cell and RefCell
      1. Cell
      2. What's the point?
      3. RefCell
    5. Arc
    6. Mutex and RwLock
    7. Summary
  12. Generic Types
    1. Types with generic type parameters
    2. Limiting what types can be used for type parameters
    3. Implementing functionality for types with generic type parameters
    4. Using generic types as function return values
    5. Compiler errors involving generic type parameters
    6. Generic types on functions outside of implementation blocks
    7. Alternative ways to write trait bounds
    8. Generic types versus trait objects
    9. Higher-order functions and trait bounds that represent functions
    10. Complete implementation of a binary tree with generic type parameters
    11. Summary
  13. Important Standard Traits
    1. Traits that can be derived
      1. Clone
      2. Copy
      3. Debug
      4. PartialEq
      5. Eq
      6. PartialOrd
      7. Ord
      8. Hash
      9. Default
    2. Traits that enable operators
      1. ​Add, Mul, Sub, and Div
      2. AddAssign, MulAssign, SubAssign, and DivAssign
      3. BitAnd
      4. BitAndAssign
      5. BitOr
      6. BitOrAssign
      7. BitXor
      8. BitXorAssign
      9. Deref
      10. DerefMut
      11. Drop
      12. Index
      13. IndexMut
      14. Neg
      15. Not
      16. Rem and RemAssign
      17. Shl and ShlAssign
      18. Shr and ShrAssign
    3. Traits that are implemented automatically
      1. Sync
      2. Send
      3. Sized
      4. Fn
      5. FnMut
      6. FnOnce
    4. Summary
  14. Other Books You May Enjoy
    1. Leave a review - let other readers know what you think

Product information

  • Title: Rust Quick Start Guide
  • Author(s): Daniel Arbuckle
  • Release date: October 2018
  • Publisher(s): Packt Publishing
  • ISBN: 9781789616705