Rust in Action is a hands-on guide to systems programming with Rust. Written for inquisitive programmers, it presents real-world use cases that go far beyond syntax and structure.
Rust in Action introduces the Rust programming language by exploring numerous systems programming concepts and techniques. You’ll be learning Rust by delving into how computers work under the hood. You’ll find yourself playing with persistent storage, memory, networking and even tinkering with CPU instructions. The book takes you through using Rust to extend other applications and teaches you tricks to write blindingly fast code. You’ll also discover parallel and concurrent programming. Filled to the brim with real-life use cases and scenarios, you’ll go beyond the Rust syntax and see what Rust has to offer in real-world use cases.
Purchase of the print book includes a free eBook in PDF, Kindle, and ePub formats from Manning Publications.
About the technology
Rust is the perfect language for systems programming. It delivers the low-level power of C along with rock-solid safety features that let you code fearlessly. Ideal for applications requiring concurrency, Rust programs are compact, readable, and blazingly fast. Best of all, Rust’s famously smart compiler helps you avoid even subtle coding errors.
About the book
Rust in Action is a hands-on guide to systems programming with Rust. Written for inquisitive programmers, it presents real-world use cases that go far beyond syntax and structure. You’ll explore Rust implementations for file manipulation, networking, and kernel-level programming and discover awesome techniques for parallelism and concurrency. Along the way, you’ll master Rust’s unique borrow checker model for memory management without a garbage collector.
Elementary to advanced Rust programming
Practical examples from systems programming
Command-line, graphical and networked applications
About the reader
For intermediate programmers. No previous experience with Rust required.
About the author
Tim McNamara uses Rust to build data processing pipelines and generative art. He is an expert in natural language processing and data engineering.
Table of Contents
1 Introducing Rust
PART 1 RUST LANGUAGE DISTINCTIVES
2 Language foundations
3 Compound data types
4 Lifetimes, ownership, and borrowing
PART 2 DEMYSTIFYING SYSTEMS PROGRAMMING
5 Data in depth
7 Files and storage
9 Time and timekeeping
10 Processes, threads, and containers
12 Signals, interrupts, and exceptions
inside front cover Rust in Action Copyright dedication contents front matter preface acknowledgments about this book Who should read this book How this book is organized: A roadmap About the code liveBook discussion forum Other online resources about the author about the cover illustration 1 Introducing Rust 1.1 Where is Rust used? 1.2 Advocating for Rust at work 1.3 A taste of the language 1.3.1 Cheating your way to “Hello, world!” 1.3.2 Your first Rust program 1.4 Downloading the book’s source code 1.5 What does Rust look and feel like? 1.6 What is Rust? 1.6.1 Goal of Rust: Safety 1.6.2 Goal of Rust: Productivity 1.6.3 Goal of Rust: Control 1.7 Rust’s big features 1.7.1 Performance 1.7.2 Concurrency 1.7.3 Memory efficiency 1.8 Downsides of Rust 1.8.1 Cyclic data structures 1.8.2 Compile times 1.8.3 Strictness 1.8.4 Size of the language 1.8.5 Hype 1.9 TLS security case studies 1.9.1 Heartbleed 1.9.2 Goto fail; 1.10 Where does Rust fit best? 1.10.1 Command-line utilities 1.10.2 Data processing 1.10.3 Extending applications 1.10.4 Resource-constrained environments 1.10.5 Server-side applications 1.10.6 Desktop applications 1.10.7 Desktop 1.10.8 Mobile 1.10.9 Web 1.10.10 Systems programming 1.11 Rust’s hidden feature: Its community 1.12 Rust phrase book Summary Part 1 Rust language distinctives 2 Language foundations 2.1 Creating a running program 2.1.1 Compiling single files with rustc 2.1.2 Compiling Rust projects with cargo 2.2 A glance at Rust’s syntax 2.2.1 Defining variables and calling functions 2.3 Numbers 2.3.1 Integers and decimal (floating-point) numbers 2.3.2 Integers with base 2, base 8, and base 16 notation 2.3.3 Comparing numbers 2.3.4 Rational, complex numbers, and other numeric types 2.4 Flow control 2.4.1 For: The central pillar of iteration 2.4.2 Continue: Skipping the rest of the current iteration 2.4.3 While: Looping until a condition changes its state 2.4.4 Loop: The basis for Rust’s looping constructs 2.4.5 Break: Aborting a loop 2.4.6 If, if else, and else: Conditional branching 2.4.7 Match: Type-aware pattern matching 2.5 Defining functions 2.6 Using references 2.7 Project: Rendering the Mandelbrot set 2.8 Advanced function definitions 2.8.1 Explicit lifetime annotations 2.8.2 Generic functions 2.9 Creating grep-lite 2.10 Making lists of things with arrays, slices, and vectors 2.10.1 Arrays 2.10.2 Slices 2.10.3 Vectors 2.11 Including third-party code 2.11.1 Adding support for regular expressions 2.11.2 Generating the third-party crate documentation locally 2.11.3 Managing Rust toolchains with rustup 2.12 Supporting command-line arguments 2.13 Reading from files 2.14 Reading from stdin Summary 3 Compound data types 3.1 Using plain functions to experiment with an API 3.2 Modeling files with struct 3.3 Adding methods to a struct with impl 3.3.1 Simplifying object creation by implementing new() 3.4 Returning errors 3.4.1 Modifying a known global variable 3.4.2 Making use of the Result return type 3.5 Defining and making use of an enum 3.5.1 Using an enum to manage internal state 3.6 Defining common behavior with traits 3.6.1 Creating a Read trait 3.6.2 Implementing std::fmt::Display for your own types 3.7 Exposing your types to the world 3.7.1 Protecting private data 3.8 Creating inline documentation for your projects 3.8.1 Using rustdoc to render docs for a single source file 3.8.2 Using cargo to render docs for a crate and its dependencies Summary 4 Lifetimes, ownership, and borrowing 4.1 Implementing a mock CubeSat ground station 4.1.1 Encountering our first lifetime issue 4.1.2 Special behavior of primitive types 4.2 Guide to the figures in this chapter 4.3 What is an owner? Does it have any responsibilities? 4.4 How ownership moves 4.5 Resolving ownership issues 4.5.1 Use references where full ownership is not required 4.5.2 Use fewer long-lived values 4.5.3 Duplicate the value 4.5.4 Wrap data within specialty types Summary Part 2 Demystifying systems programming 5 Data in depth 5.1 Bit patterns and types 5.2 Life of an integer 5.2.1 Understanding endianness 5.3 Representing decimal numbers 5.4 Floating-point numbers 5.4.1 Looking inside an f32 5.4.2 Isolating the sign bit 5.4.3 Isolating the exponent 5.4.4 Isolate the mantissa 5.4.5 Dissecting a floating-point number 5.5 Fixed-point number formats 5.6 Generating random probabilities from random bytes 5.7 Implementing a CPU to establish that functions are also data 5.7.1 CPU RIA/1: The Adder 5.7.2 Full code listing for CPU RIA/1: The Adder 5.7.3 CPU RIA/2: The Multiplier 5.7.4 CPU RIA/3: The Caller 5.7.5 CPU 4: Adding the rest Summary 6 Memory 6.1 Pointers 6.2 Exploring Rust’s reference and pointer types 6.2.1 Raw pointers in Rust 6.2.2 Rust’s pointer ecosystem 6.2.3 Smart pointer building blocks 6.3 Providing programs with memory for their data 6.3.1 The stack 6.3.2 The heap 6.3.3 What is dynamic memory allocation? 6.3.4 Analyzing the impact of dynamic memory allocation 6.4 Virtual memory 6.4.1 Background 6.4.2 Step 1: Having a process scan its own memory 6.4.3 Translating virtual addresses to physical addresses 6.4.4 Step 2: Working with the OS to scan an address space 6.4.5 Step 3: Reading from and writing to process memory Summary 7 Files and storage 7.1 What is a file format? 7.2 Creating your own file formats for data storage 7.2.1 Writing data to disk with serde and the bincode format 7.3 Implementing a hexdump clone 7.4 File operations in Rust 7.4.1 Opening a file in Rust and controlling its file mode 7.4.2 Interacting with the filesystem in a type-safe manner with std::fs::Path 7.5 Implementing a key-value store with a log-structured, append-only storage architecture 7.5.1 The key-value model 7.5.2 Introducing actionkv v1: An in-memory key-value store with a command-line interface 7.6 Actionkv v1: The front-end code 7.6.1 Tailoring what is compiled with conditional compilation 7.7 Understanding the core of actionkv: The libactionkv crate 7.7.1 Initializing the ActionKV struct 7.7.2 Processing an individual record 7.7.3 Writing multi-byte binary data to disk in a guaranteed byte order 7.7.4 Validating I/O errors with checksums 7.7.5 Inserting a new key-value pair into an existing database 7.7.6 The full code listing for actionkv 7.7.7 Working with keys and values with HashMap and BTreeMap 7.7.8 Creating a HashMap and populating it with values 7.7.9 Retrieving values from HashMap and BTreeMap 7.7.10 How to decide between HashMap and BTreeMap 7.7.11 Adding a database index to actionkv v2.0 Summary 8 Networking 8.1 All of networking in seven paragraphs 8.2 Generating an HTTP GET request with reqwest 8.3 Trait objects 8.3.1 What do trait objects enable? 8.3.2 What is a trait object? 8.3.3 Creating a tiny role-playing game: The rpg project 8.4 TCP 8.4.1 What is a port number? 8.4.2 Converting a hostname to an IP address 8.5 Ergonomic error handling for libraries 8.5.1 Issue: Unable to return multiple error types 8.5.2 Wrapping downstream errors by defining our own error type 8.5.3 Cheating with unwrap() and expect() 8.6 MAC addresses 8.6.1 Generating MAC addresses 8.7 Implementing state machines with Rust’s enums 8.8 Raw TCP 8.9 Creating a virtual networking device 8.10 “Raw” HTTP Summary 9 Time and timekeeping 9.1 Background 9.2 Sources of time 9.3 Definitions 9.4 Encoding time 9.4.1 Representing time zones 9.5 clock v0.1.0: Teaching an application how to tell the time 9.6 clock v0.1.1: Formatting timestamps to comply with ISO 8601 and email standards 9.6.1 Refactoring the clock v0.1.0 code to support a wider architecture 9.6.2 Formatting the time 9.6.3 Providing a full command-line interface 9.6.4 clock v0.1.1: Full project 9.7 clock v0.1.2: Setting the time 9.7.1 Common behavior 9.7.2 Setting the time for operating systems that use libc 9.7.3 Setting the time on MS Windows 9.7.4 clock v0.1.2: The full code listing 9.8 Improving error handling 9.9 clock v0.1.3: Resolving differences between clocks with the Network Time Protocol (NTP) 9.9.1 Sending NTP requests and interpreting responses 9.9.2 Adjusting the local time as a result of the server’s response 9.9.3 Converting between time representations that use different precisions and epochs 9.9.4 clock v0.1.3: The full code listing Summary 10 Processes, threads, and containers 10.1 Anonymous functions 10.2 Spawning threads 10.2.1 Introduction to closures 10.2.2 Spawning a thread 10.2.3 Effect of spawning a few threads 10.2.4 Effect of spawning many threads 10.2.5 Reproducing the results 10.2.6 Shared variables 10.3 Differences between closures and functions 10.4 Procedurally generated avatars from a multithreaded parser and code generator 10.4.1 How to run render-hex and its intended output 10.4.2 Single-threaded render-hex overview 10.4.3 Spawning a thread per logical task 10.4.4 Using a thread pool and task queue 10.5 Concurrency and task virtualization 10.5.1 Threads 10.5.2 What is a context switch? 10.5.3 Processes 10.5.4 WebAssembly 10.5.5 Containers 10.5.6 Why use an operating system (OS) at all? Summary 11 Kernel 11.1 A fledgling operating system (FledgeOS) 11.1.1 Setting up a development environment for developing an OS kernel 11.1.2 Verifying the development environment 11.2 Fledgeos-0: Getting something working 11.2.1 First boot 11.2.2 Compilation instructions 11.2.3 Source code listings 11.2.4 Panic handling 11.2.5 Writing to the screen with VGA-compatible text mode 11.2.6 _start(): The main() function for FledgeOS 11.3 fledgeos-1: Avoiding a busy loop 11.3.1 Being power conscious by interacting with the CPU directly 11.3.2 fledgeos-1 source code 11.4 fledgeos-2: Custom exception handling 11.4.1 Handling exceptions properly, almost 11.4.2 fledgeos-2 source code 11.5 fledgeos-3: Text output 11.5.1 Writing colored text to the screen 11.5.2 Controlling the in-memory representation of enums 11.5.3 Why use enums? 11.5.4 Creating a type that can print to the VGA frame buffer 11.5.5 Printing to the screen 11.5.6 fledgeos-3 source code 11.6 fledgeos-4: Custom panic handling 11.6.1 Implementing a panic handler that reports the error to the user 11.6.2 Reimplementing panic() by making use of core::fmt::Write 11.6.3 Implementing core::fmt::Write 11.6.4 fledge-4 source code Summary 12 Signals, interrupts, and exceptions 12.1 Glossary 12.1.1 Signals vs. interrupts 12.2 How interrupts affect applications 12.3 Software interrupts 12.4 Hardware interrupts 12.5 Signal handling 12.5.1 Default behavior 12.5.2 Suspend and resume a program’s operation 12.5.3 Listing all signals supported by the OS 12.6 Handling signals with custom actions 12.6.1 Global variables in Rust 12.6.2 Using a global variable to indicate that shutdown has been initiated 12.7 Sending application-defined signals 12.7.1 Understanding function pointers and their syntax 12.8 Ignoring signals 12.9 Shutting down from deeply nested call stacks 12.9.1 Introducing the sjlj project 12.9.2 Setting up intrinsics in a program 12.9.3 Casting a pointer to another type 12.9.4 Compiling the sjlj project 12.9.5 sjlj project source code 12.10 A note on applying these techniques to platforms without signals 12.11 Revising exceptions Summary index
How to download source code?
1. Go to:
2. Search the book title:
Rust in Action, sometime you may not get the results, please search the main title
3. Click the book title in the search results
resources section, click
1. Disable the AdBlock plugin. Otherwise, you may not get any links.
2. Solve the CAPTCHA.
3. Click download link.
4. Lead to download server to download.