Practical System Programming for Rust Developers

Book description

Explore various Rust features, data structures, libraries, and toolchain to build modern systems software with the help of hands-on examples

Key Features

  • Learn techniques to design and build system tools and utilities in Rust
  • Explore the different features of the Rust standard library for interacting with operating systems
  • Gain an in-depth understanding of the Rust programming language by writing low-level software

Book Description

Modern programming languages such as Python, JavaScript, and Java have become increasingly accepted for application-level programming, but for systems programming, C and C++ are predominantly used due to the need for low-level control of system resources. Rust promises the best of both worlds: the type safety of Java, and the speed and expressiveness of C++, while also including memory safety without a garbage collector. This book is a comprehensive introduction if you're new to Rust and systems programming and are looking to build reliable and efficient systems software without C or C++.

The book takes a unique approach by starting each topic with Linux kernel concepts and APIs relevant to that topic. You'll also explore how system resources can be controlled from Rust. As you progress, you'll delve into advanced topics. You'll cover network programming, focusing on aspects such as working with low-level network primitives and protocols in Rust, before going on to learn how to use and compile Rust with WebAssembly. Later chapters will take you through practical code examples and projects to help you build on your knowledge.

By the end of this Rust programming book, you will be equipped with practical skills to write systems software tools, libraries, and utilities in Rust.

What you will learn

  • Gain a solid understanding of how system resources are managed
  • Use Rust confidently to control and operate a Linux or Unix system
  • Understand how to write a host of practical systems software tools and utilities
  • Delve into memory management with the memory layout of Rust programs
  • Discover the capabilities and features of the Rust Standard Library
  • Explore external crates to improve productivity for future Rust programming projects

Who this book is for

This book is for developers with basic knowledge of Rust but little to no knowledge or experience of systems programming. System programmers who want to consider Rust as an alternative to C or C++ will also find this book useful.

Publisher resources

Download Example Code

Table of contents

  1. Practical System Programming for Rust Developers
  2. Why subscribe?
  3. Contributors
  4. About the author
  5. About the reviewer
  6. Packt is searching for authors like you
  7. Preface
    1. Who this book is for
    2. What this book covers
    3. To get the most out of this book
    4. Download the example code files
    5. Download the color images
    6. Conventions used
    7. Get in touch
    8. Reviews
  8. Section 1: Getting Started with System Programming in Rust
  9. Chapter 1: Tools of the Trade – Rust Toolchains and Project Structures
    1. Technical requirements
    2. Choosing the right Rust configuration for your project
      1. Choosing a Rust release channel
      2. Selecting a Rust project type
    3. Introducing Cargo and project structures
    4. Automating build management with Cargo
      1. Building a basic binary crate
      2. Configuring Cargo
      3. Building a static library crate
    5. Automating dependency management
      1. Specifying the location of a dependency
      2. Using dependent packages in source code
    6. Writing and running automated tests
      1. Writing unit tests in Rust
      2. Writing integration tests in Rust
      3. Controlling test execution
      4. Running tests sequentially or in parallel
    7. Documenting your project
      1. Writing inline documentation comments within crate
      2. Writing documentation in markdown files
      3. Running documentation tests
    8. Summary
    9. Further reading
  10. Chapter 2: A Tour of the Rust Programming Language
    1. Technical requirements
    2. Analyzing the problem domain
    3. Modeling the system behavior
    4. Building the tokenizer
      1. Tokenizer data structure
      2. Tokenizer data processing
    5. Building the parser
      1. Parser data structure
      2. Parser methods
      3. Operator precedence
    6. Building the evaluator
    7. Dealing with errors
    8. Putting it all together
    9. Summary
  11. Chapter 3: Introduction to the Rust Standard Library
    1. Technical requirements
    2. The Rust Standard Library and systems programming
    3. Exploring the Rust Standard Library
      1. Computation-oriented modules
      2. Syscalls-oriented modules
    4. Building a template engine
      1. Template syntax and design
      2. Writing the template engine
      3. Executing the template engine
    5. Summary
    6. Further reading
  12. Chapter 4: Managing Environment, Command Line, and Time
    1. Technical requirements
    2. Project scope and design overview
      1. What will we build?
      2. Technical design
      3. Using the Rust Standard Library
    3. Coding the imagix library
    4. Developing the command-line application and testing
      1. Designing the command-line interface
      2. Coding the command-line binary using structopt
    5. Summary
  13. Section 2: Managing and Controlling System Resources in Rust
  14. Chapter 5: Memory Management in Rust
    1. Technical requirements
    2. The basics of OS memory management
      1. The memory management lifecycle
      2. The process memory layout
    3. Understanding the memory layout of Rust programs
      1. Rust program memory layout
      2. The characteristics of stack, heap, and static memory
    4. The Rust memory management lifecycle
      1. Overview of the Rust memory management lifecycle
      2. Memory allocation
      3. Memory use and manipulation
      4. Memory deallocation
    5. Implementing a dynamic data structure
      1. Changes to the design of the template engine
      2. Coding the dynamic data structure
    6. Summary
    7. Further reading
  15. Chapter 6: Working with Files and Directories in Rust
    1. Technical requirements
    2. Understanding Linux system calls for file operations
    3. Doing file I/O in Rust
    4. Learning directory and path operations
    5. Setting hard links, symbolic links, and performing queries
    6. Writing a shell command in Rust (project)
      1. Code overview
      2. Error handling
      3. Source metric computation
      4. The main() function
    7. Summary
  16. Chapter 7: Implementing Terminal I/O in Rust
    1. Technical requirements
    2. Introducing terminal I/O fundamentals
      1. Characteristics of terminals
      2. The Termion crate
      3. What will we build?
    3. Working with the terminal UI (size, color, styles) and cursors
      1. Writing data structures and the main() function
      2. Initializing the text viewer and getting the terminal size
      3. Displaying a document and styling the terminal color, styles, and cursor position
      4. Exiting the text viewer
    4. Processing keyboard inputs and scrolling
      1. Listening to keystrokes from the user
      2. Positioning the terminal cursor
      3. Enabling scrolling on the terminal
    5. Processing mouse inputs
    6. Summary
  17. Chapter 8: Working with Processes and Signals
    1. Technical requirements
    2. Understanding Linux process concepts and syscalls
      1. How does a program become a process?
      2. Delving into Linux process fundamentals
    3. Spawning processes with Rust
      1. Spawning new child processes
      2. Terminating processes
      3. Checking the status of a child process' execution
    4. Handling I/O and environment variables
      1. Handling the I/O of child processes
      2. Setting the environment for the child process
    5. Handling panic, errors, and signals
      1. Aborting the current process
      2. Signal handling
    6. Writing a shell program in Rust (project)
    7. Summary
  18. Chapter 9: Managing Concurrency
    1. Technical requirements
    2. Reviewing concurrency basics
      1. Concurrency versus parallelism
      2. Concepts of multi-threading
    3. Spawning and configuring threads
    4. Error handling in threads
    5. Message passing between threads
    6. Achieving concurrency with shared state
      1. Defining the program structure
      2. Aggregating source file statistics in shared state
    7. Pausing thread execution with timers
    8. Summary
  19. Section 3: Advanced Topics
  20. Chapter 10: Working with Device I/O
    1. Technical requirements
    2. Understanding device I/O fundamentals in Linux
      1. What are device drivers?
      2. Types of devices
    3. Doing buffered reads and writes
    4. Working with standard input and output
    5. Chaining and iterators over I/O
    6. Handling errors and returning values
    7. Getting details of connected USB devices (project)
      1. Designing the project
      2. Writing data structures and utility functions
      3. Writing the main() function
    8. Summary
  21. Chapter 11: Learning Network Programming
    1. Technical requirements
    2. Reviewing networking basics in Linux
    3. Understanding networking primitives in the Rust standard library
    4. Programming with TCP and UDP in Rust
      1. Writing a UDP server and client
      2. Writing a TCP server and client
    5. Writing a TCP reverse proxy (project)
      1. Writing the origin server – structs and methods
      2. Writing the origin server – the main() function
      3. Writing the reverse proxy server
    6. Summary
  22. Chapter 12: Writing Unsafe Rust and FFI
    1. Technical requirements
    2. Introducing unsafe Rust
      1. How do you distinguish between safe and unsafe Rust code?
      2. Operations in unsafe Rust
    3. Introducing FFIs
    4. Reviewing guidelines for safe FFIs
    5. Calling Rust from C (project)
    6. Understanding the ABI
    7. Summary
  23. Other Books You May Enjoy
    1. Leave a review - let other readers know what you think

Product information

  • Title: Practical System Programming for Rust Developers
  • Author(s): Prabhu Eshwarla
  • Release date: December 2020
  • Publisher(s): Packt Publishing
  • ISBN: 9781800560963