Creative Projects for Rust Programmers

Book description

A practical guide to understanding the latest features of the Rust programming language, useful libraries, and frameworks that will help you design and develop interesting projects

Key Features

  • Work through projects that will help you build high-performance applications with Rust
  • Delve into concepts such as error handling, memory management, concurrency, generics, and macros with Rust
  • Improve business productivity by choosing the right libraries and frameworks for your applications

Book Description

Rust is a community-built language that solves pain points present in many other languages, thus improving performance and safety. In this book, you will explore the latest features of Rust by building robust applications across different domains and platforms.

The book gets you up and running with high-quality open source libraries and frameworks available in the Rust ecosystem that can help you to develop efficient applications with Rust. You'll learn how to build projects in domains such as data access, RESTful web services, web applications, 2D games for web and desktop, interpreters and compilers, emulators, and Linux Kernel modules. For each of these application types, you'll use frameworks such as Actix, Tera, Yew, Quicksilver, ggez, and nom. This book will not only help you to build on your knowledge of Rust but also help you to choose an appropriate framework for building your project.

By the end of this Rust book, you will have learned how to build fast and safe applications with Rust and have the real-world experience you need to advance in your career.

What you will learn

  • Access TOML, JSON, and XML files and SQLite, PostgreSQL, and Redis databases
  • Develop a RESTful web service using JSON payloads
  • Create a web application using HTML templates and JavaScript and a frontend web application or web game using WebAssembly
  • Build desktop 2D games
  • Develop an interpreter and a compiler for a programming language
  • Create a machine language emulator
  • Extend the Linux Kernel with loadable modules

Who this book is for

This Rust programming book is for developers who want to get hands-on experience with implementing their knowledge of Rust programming, and are looking for expert advice on which libraries and frameworks they can adopt to develop software that typically uses the Rust language.

Table of contents

  1. Title Page
  2. Copyright and Credits
    1. Creative Projects for Rust Programmers
  3. Dedication
  4. About Packt
    1. Why subscribe?
  5. Contributors
    1. About the author
    2. About the reviewer
    3. Packt is searching for authors like you
  6. 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
  7. Rust 2018: Productivity
    1. Technical requirements
    2. Understanding the different editions of Rust
    3. The projects
    4. Working through the examples in this book
    5. Exploring some utility crates
      1. Pseudo-random number generators – the rand crate
      2. Logging – the log crate
      3. Initializing static variables at runtime – the lazy_static crate
      4. Parsing the command line – the structopt crate
    6. Summary
    7. Questions
  8. Storing and Retrieving Data
    1. Technical requirements
    2. Project overview
    3. Reading a TOML file
      1. Using toml_dynamic
      2. Using toml_static
    4. Reading and writing a JSON file
      1. The json_dynamic project
      2. The json_static project
    5. Reading an XML file
    6. Accessing databases
    7. Accessing a SQLite database
      1. Implementing the project
    8. Accessing a PostgreSQL database
      1. Implementation of the project
    9. Storing and retrieving data from a Redis store
      1. Implementing the project
    10. Putting it all together
    11. Summary
    12. Questions
  9. Creating a REST Web Service
    1. Technical requirements
    2. The REST architecture
    3. Project overview
    4. Essential background theory and context
    5. Building a stub of a REST web service
      1. Running and testing the service
      2. Getting a resource using the GET method
      3. Sending a named resource to the server using the PUT method
      4. Sending a new resource to the server using the POST method
      5. Deleting a resource using the DELETE method
      6. Sending an invalid command
      7. Examining the code
    6. Building a complete web service
      1. Downloading a file
      2. Uploading a string to a specified file
      3. Uploading a string to a new file
      4. Deleting a file
      5. Examining the code
    7. Building a stateful server
      1. How to have a stateful server
      2. The API of this service
      3. Testing the service
      4. Implementing the database
      5. Handling queries
    8. Returning JSON data
    9. Summary
    10. Questions
    11. Further reading
  10. Creating a Full Server-Side Web App
    1. Technical requirements
    2. Definition of a web app
    3. Understanding the behavior of a web app
    4. Project overview
    5. Using the Tera template engine
    6. A simple list of persons
      1. The templates folder
      2. The other Rust handlers
    7. A CRUD application
      1. The JavaScript code
      2. The HTML code
      3. The Rust code
        1. The db_access.rs changes
        2. The main.rs changes
    8. Handling an application with authentication
      1. The implementation
      2. The HTML code
      3. The JavaScript code
      4. The mock database code
      5. The main function
    9. Summary
    10. Questions
    11. Further reading
  11. Creating a Client-Side WebAssembly App Using Yew
    1. Technical requirements
    2. Introducing Wasm
    3. Understanding the MVC architectural pattern
      1. Implementing two toy web apps
      2. What is the MVC pattern?
      3. The model
      4. The view
      5. The controller
      6. View implementation
      7. Controller implementation
      8. Understanding the MVC architecture
    4. Project overview
      1. Getting started
    5. The incr app
    6. The adder app
    7. The login app
      1. Organization of the project
        1. The db_access.rs file
        2. The main.rs file
        3. The login.rs file
    8. The yauth app
      1. Understanding the behavior of the app
      2. Organization of the project
        1. The persons_list.rs file
          1. Why an empty collection wouldn't be good for filtered_persons
        2. The one_person.rs file
    9. A web app accessing a RESTful service
      1. The persons_db app
      2. The yclient app
        1. The imported crates
        2. The source files
        3. The changes to the models
        4. A typical client/server request
    10. Summary
    11. Questions
    12. Further reading
  12. Creating a WebAssembly Game Using Quicksilver
    1. Technical requirements
    2. Project overview
    3. Understanding the animation loop architecture
    4. Implementing the ski project
      1. Understanding the code behind this
    5. Implementing the silent_slalom project
    6. Implementing the assets_slalom project
      1. Analyzing the code
    7. Summary
    8. Questions
    9. Further reading
  13. Creating a Desktop Two-Dimensional Game Using ggez
    1. Technical requirements
    2. Project overview
    3. Understanding linear algebra
    4. Implementing the gg_ski project
      1. The main function
      2. Patterns of input handling
      3. Input handling in the gg_ski project
      4. Other differences with quicksilver
        1. Name of the trait
        2. The type of context
        3. The new method
        4. The angle's unit of measurement
        5. How to specify the FPS rate
        6. Handling the ski steering
        7. Computation of new position and speed
        8. Drawing the background
        9. Drawing composite shapes
        10. Ending the draw method
    5. Implementing the gg_silent_slalom project
    6. Implementing the gg_assets_slalom project
    7. Implementing the gg_whac project
      1. The assets
      2. The general structure of the application and events
      3. Other fields of the model
      4. Defining a widget
    8. Summary
    9. Questions
    10. Further reading
  14. Using a Parser Combinator for Interpreting and Compiling
    1. Technical requirements
    2. Project overview
    3. Introducing Calc
    4. Understanding formal languages and their parsers
      1. Regular languages
      2. Context-free languages
      3. Context-dependent languages
    5. Using Nom to build parsers
      1. Learning about compiler-compilers and parser combinators
      2. Learning the basics of Nom
        1. Parsing an alternative of characters
        2. Parsing a sequence of characters
        3. Parsing a fixed string
        4. Mapping parsed items to other objects
        5. Creating custom parsing results
        6. Parsing a variable text
        7. Repeating a parser
    6. The calc_parser project
      1. Understanding the main.rs source file
      2. Learning about the parser.rs source file
        1. Understanding the types needed by the parser
        2. Looking at the parser code
    7. The calc_analyzer project
      1. Checking the variables of the parsed program
      2. Understanding the main.rs file
      3. Looking at the symbol_table.rs file
      4. Glancing at the analyzer.rs file
    8. The calc_interpreter project 
      1. Learning about the main.rs file
      2. Glancing at the symbol_table.rs file
      3. Understanding the executor.rs file
    9. The calc_compiler project
      1. Glancing at the main.rs file
      2. Understanding the compiler.rs file
    10. Summary
    11. Questions
    12. Further reading
  15. Creating a Computer Emulator Using Nom
    1. Technical requirements
    2. Project overview
    3. Introducing a very simple machine language
      1. The most important concepts relating to machine language
      2. Extending our machine language
      3. Writing a very simple program
      4. A more complex program – the sieve of Eratosthenes
    4. Defining a byte-addressing machine language
      1. Coping with the endianness issue
    5. The nom_byte_machine project
      1. Understanding the main.rs source file
      2. Using the Nom library
      3. The emulator.rs source file
      4. The parsing_interpreter.rs source file
      5. The translator.rs source file
    6. The nom_disassembler project
      1. Running the project
      2. Examining the source code
      3. Generating disassembly code that is useful for debugging
      4. Generating disassembly code that is useful for reassembling
    7. Summary
    8. Questions
  16. Creating a Linux Kernel Module
    1. Technical requirements
    2. Project overview
    3. Understanding kernel modules
      1. Preparing the environment
    4. A boilerplate module
      1. Building and running the kernel module
      2. The build commands
      3. The source code of the boilerplate module
    5. Using global variables
    6. Allocating memory
    7. A character device
      1. Building the character device
      2. The source code of the dots module
        1. The global information
        2. The initialization call
        3. The cleanup call
        4. The reading function
    8. Summary
    9. Questions
    10. Further reading
  17. The Future of Rust
    1. IDEs and interactive programming
    2. Crate maturity
    3. Asynchronous programming
    4. Optimization
      1. The const generics feature
    5. Embedded systems
    6. Summary
  18. Assessments
    1. Chapter 1
    2. Chapter 2
    3. Chapter 3
    4. Chapter 4
    5. Chapter 5
    6. Chapter 6
    7. Chapter 7
    8. Chapter 8
    9. Chapter 9
    10. Chapter 10
  19. Other Books You May Enjoy
    1. Leave a review - let other readers know what you think

Product information

  • Title: Creative Projects for Rust Programmers
  • Author(s): Carlo Milanesi
  • Release date: June 2020
  • Publisher(s): Packt Publishing
  • ISBN: 9781789346220