Practical System Programming for Rust Developers....2
Why subscribe?....3
Contributors....3
About the author....3
About the reviewer....4
Packt is searching for authors like you....4
Preface....25
Who this book is for....26
What this book covers....27
To get the most out of this book....27
Download the example code files....28
Download the color images....29
Conventions used....29
Get in touch....29
Reviews....30
Section 1: Getting Started with System Programming in Rust....31
Chapter 1: Tools of the Trade – Rust Toolchains and Project Structures....32
Technical requirements....32
Choosing the right Rust configuration for your project....32
Choosing a Rust release channel....33
Selecting a Rust project type....34
Introducing Cargo and project structures....35
Automating build management with Cargo....36
Building a basic binary crate....36
Configuring Cargo....38
Building a static library crate....39
Automating dependency management....40
Specifying the location of a dependency....41
Using dependent packages in source code....41
Writing and running automated tests....42
Writing unit tests in Rust....42
Writing integration tests in Rust....44
Controlling test execution....45
Running tests sequentially or in parallel....46
Documenting your project....47
Writing inline documentation comments within crate....47
Writing documentation in markdown files....48
Running documentation tests....49
Summary....49
Further reading....50
Chapter 2: A Tour of the Rust Programming Language....51
Technical requirements....51
Analyzing the problem domain....51
Modeling the system behavior....52
Building the tokenizer....55
Tokenizer data structure....56
Tokenizer data processing....59
Building the parser....63
Parser data structure....63
Parser methods....65
Operator precedence....70
Building the evaluator....73
Dealing with errors....74
Putting it all together....77
Summary....79
Chapter 3: Introduction to the Rust Standard Library....80
Technical requirements....80
The Rust Standard Library and systems programming....80
Exploring the Rust Standard Library....82
Computation-oriented modules....86
Syscalls-oriented modules....88
Building a template engine....91
Template syntax and design....93
Writing the template engine....99
Executing the template engine....107
Summary....108
Further reading....108
Chapter 4: Managing Environment, Command Line, and Time....109
Technical requirements....110
Project scope and design overview....110
What will we build?....110
Technical design....113
Using the Rust Standard Library....114
Coding the imagix library....120
Developing the command-line application and testing....126
Designing the command-line interface....128
Coding the command-line binary using structopt....129
Summary....132
Section 2: Managing and Controlling System Resources in Rust....133
Chapter 5: Memory Management in Rust....134
Technical requirements....135
The basics of OS memory management....135
The memory management lifecycle....135
The process memory layout....138
Understanding the memory layout of Rust programs....139
Rust program memory layout....139
The characteristics of stack, heap, and static memory....141
The Rust memory management lifecycle....143
Overview of the Rust memory management lifecycle....144
Memory allocation....145
Memory use and manipulation....146
Memory deallocation....148
Implementing a dynamic data structure....150
Changes to the design of the template engine....151
Coding the dynamic data structure....153
Summary....157
Further reading....157
Chapter 6: Working with Files and Directories in Rust....158
Technical requirements....158
Understanding Linux system calls for file operations....158
Doing file I/O in Rust....160
Learning directory and path operations....165
Setting hard links, symbolic links, and performing queries....168
Writing a shell command in Rust (project)....169
Code overview....169
Error handling....171
Source metric computation....172
The main() function....176
Summary....178
Chapter 7: Implementing Terminal I/O in Rust....179
Technical requirements....179
Introducing terminal I/O fundamentals....179
Characteristics of terminals....180
The Termion crate....180
What will we build?....182
Working with the terminal UI (size, color, styles) and cursors....183
Writing data structures and the main() function....183
Initializing the text viewer and getting the terminal size....186
Displaying a document and styling the terminal color, styles, and cursor position....187
Exiting the text viewer....188
Processing keyboard inputs and scrolling....189
Listening to keystrokes from the user....190
Positioning the terminal cursor....191
Enabling scrolling on the terminal....192
Processing mouse inputs....194
Summary....196
Chapter 8: Working with Processes and Signals....198
Technical requirements....198
Understanding Linux process concepts and syscalls....199
How does a program become a process?....199
Delving into Linux process fundamentals....201
Spawning processes with Rust....203
Spawning new child processes....203
Terminating processes....205
Checking the status of a child process' execution....206
Handling I/O and environment variables....207
Handling the I/O of child processes....207
Setting the environment for the child process....209
Handling panic, errors, and signals....210
Aborting the current process....210
Signal handling....211
Writing a shell program in Rust (project)....213
Summary....218
Chapter 9: Managing Concurrency....220
Technical requirements....220
Reviewing concurrency basics....220
Concurrency versus parallelism....221
Concepts of multi-threading....223
Spawning and configuring threads....225
Error handling in threads....227
Message passing between threads....229
Achieving concurrency with shared state....231
Defining the program structure....232
Aggregating source file statistics in shared state....234
Pausing thread execution with timers....237
Summary....238
Section 3: Advanced Topics....239
Chapter 10: Working with Device I/O....240
Technical requirements....240
Understanding device I/O fundamentals in Linux....241
What are device drivers?....241
Types of devices....242
Doing buffered reads and writes....242
Working with standard input and output....244
Chaining and iterators over I/O....246
Handling errors and returning values....248
Getting details of connected USB devices (project)....250
Designing the project....250
Writing data structures and utility functions....252
Writing the main() function....254
Summary....256
Chapter 11: Learning Network Programming....257
Technical requirements....257
Reviewing networking basics in Linux....257
Understanding networking primitives in the Rust standard library....260
Programming with TCP and UDP in Rust....263
Writing a UDP server and client....263
Writing a TCP server and client....265
Writing a TCP reverse proxy (project)....266
Writing the origin server – structs and methods....268
Writing the origin server – the main() function....270
Writing the reverse proxy server....273
Summary....277
Chapter 12: Writing Unsafe Rust and FFI....279
Technical requirements....279
Introducing unsafe Rust....280
How do you distinguish between safe and unsafe Rust code?....280
Operations in unsafe Rust....281
Introducing FFIs....283
Reviewing guidelines for safe FFIs....285
Calling Rust from C (project)....286
Understanding the ABI....288
Summary....290
Other Books You May Enjoy....291
Leave a review - let other readers know what you think....292
The modern software stack is evolving rapidly in size and complexity. Technology domains such as the cloud, the web, data science, machine learning, DevOps, containers, IoT, embedded systems, distributed ledgers, virtual and augmented reality, and artificial intelligence continue to evolve and specialize. This has resulted in a severe shortage of system software developers able to build out the system infrastructure components. Modern societies, businesses, and governments increasingly rely heavily on digital technologies, which puts greater emphasis on developing safe, reliable, and efficient systems software and software infrastructure that modern web and mobile applications are built on.
System programming languages such as C/C++ have proved their mettle for decades in this domain, and provide a high degree of control and performance, but it is at the cost of memory safety.
Higher-level languages such as Java, C#, Python, Ruby, and JavaScript provide memory safety but offer less control over memory layout, and suffer from garbage collection pauses.
Rust is a modern, open source system programming language that promises the best of three worlds: the type safety of Java; the speed, expressiveness, and efficiency of C++; and memory safety without a garbage collector.
This book adopts a unique three-step approach to teaching system programming in Rust. Each chapter in this book starts with an overview of the system programming fundamentals and kernel system calls for that topic in Unix-like operating systems (Unix/ Linux/macOS). You will then learn how to perform common system calls using the Rust Standard Library, and in a few cases, external crates, using abundant code snippets. This knowledge is then reinforced through a practical example project that you will build. Lastly, there are questions in each chapter to embed learning.
By the end of this book, you will have a sound foundational understanding of how to use Rust to manage and control operating system resources such as memory, files, processes, threads, system environment, peripheral devices, networking interfaces, terminals, and shells, and you'll understand how to build cross-language bindings through FFI. Along the way, you will learn how to use the tools of the trade, and get a firm appreciation of the value Rust brings to build safe, performant, reliable, and efficient system-level software.