index
Symbols
? operator 88, 123, 212
& operator 96
Numerics
0.0.0.0 address 184
A
Actix framework 183
align property 113
all() function 117
all() predicate 57
all(predicated) attribute 21
alloc-cortex-m crate 42
allocate() method 111, 113
allocated_zeroed() method 111
Allocator API 110
Allocator trait 110 – 111, 114
allocator_api 57
allow() attribute 54
any() function 117
any() predicate 57
any(predicate) attribute 21
anyhow crate 216
API client
deciding which tools and libraries to use 205 – 206
designing CLI 206 – 207
API service
API design 184 – 185
creating architecture 184 – 185
APIs (application programming interfaces)
declaring routes 194 – 195
implementing API routes 195 – 197
application scaffolding 187 – 190
init_dbpool() function 189 – 190
init_tracing() function 188
main() function 187 – 188
arbitrary crate 152
Arc 104 – 106, 131
architecture, creating 184 – 185
args argument 148
arrays 69 – 72
as_mut() method 96
as_ref() method 96
AsMut trait 96
AsRef trait 96
assert_cmd crate 147 – 149
assert_eq!() macro 143
assert!() macro 143
assert2 crate 125
async fn syntax sugar 169
async keyword 164
async programming, thinking asynchronously 159 – 160
async Rust 157 – 181
concurrency and parallelism with 166 – 169
futures, handling async task results 161 – 164
implementing observers 169 – 174
keywords 164 – 166
mixing sync and async 174 – 176
testing async code 180 – 181
#[tokio::main] 164
tracing and debugging async code 177 – 180
when and where to use 164 – 166
when to avoid using 176 – 180
asynchronous I/O 3
avx2 target feature 117
.await feature 169
.await keyword 164 – 166
axum framework 183
axum::extract::FromRequest trait 196
axum::extract::FromRequestParts trait 196
axum::extract::Json 196
axum::extract::Path 196
axum::extract::State 196
axum::response::IntoResponse trait 197
B
binary distribution 29 – 31
building statically linked binaries 30 – 31
cross compilation 30
BinaryHeap 74
bindgen tool 29
blacklisted_name lint 52
blocking sleep 168
body field 194
bool_comparison lint 52
borrow checker 3, 96 – 97
borrowing 96
Box 72, 104
smart pointers 100 – 103
Box data structure 95
broadcast channel 176
BTreeMap 74
BTreeSet 74
build command 15
build time operations 40
build_hasher() method 76
BuildHasher API 76
built-in integration testing 146 – 150
libraries and tooling 147 – 150
assert_cmd 147 – 149
proptest 149
C
calloc() method 111
caret operator 17
Cargo 3
basic usage 12 – 13
binary distribution 29 – 31
building statically linked binaries 30 – 31
cross compilation 30
building, running, and testing 14 – 15
creating new application or library 13 – 14
dependency management 16 – 19
handling Cargo.lock file 18 – 19
- -lib argument 14
linking to C libraries 27 – 29
modules 35 – 37
patching dependencies 21 – 24
best practices for 23 – 24
indirect dependencies 23
project management with
custom building scripts 40 – 41
feature flags 19 – 21
publishing crates 24 – 27
CI/CD integration 24 – 27
- -release flag 220
rerun-if-changed 40
switching between toolchains 15 – 16
tour of 12 – 16
workspaces 38 – 39
cargo add command 17
cargo doc command 42
cargo expand 132
cargo new command 14
cargo-expand 59 – 60
cargo-fuzz 60
cargo-fuzz crate 151
cargo-tree 61 – 62
cargo-update 59
cargo-watch 60 – 61
Cargo.lock file 18 – 19
carrying_add() function 127
cc crate 40
CD (continuous deployment) 24
Cell type 105
cfg attribute 20, 57, 116 – 117
cfg macro 116 – 117
cfg(predicate) attribute 21
changing ecosystem 140
checked_add() function 127
checked_div() method 80
CI (continuous integration) 24
CI/CD integration 24 – 27
clang 233
clang-format tool 51
CLI (command-line interface)
assert_cmd 147 – 149
testing 212 – 214
client—server relationship 204
Clippy 44 – 55, 234
automatically applying suggestions 54
+channel option 15
- -check option 49
configuring 53 – 54
- -fix flag 54
installing 51
lints 52 – 53
using in CI/CD 54 – 55
clone on write 107 – 109
Clone trait 98, 107 – 108
clone() method 98, 107
closures 3
code coverage 139
Codecov 139
#[command] macro 207
commands
declaring 207 – 209
implementing 209 – 210
Commands enum 209 – 210
CommonMark 33
compile_error!() macro 122
components
installing HTTPie 235
installing rustc and other components 234
managing with rustup 234 – 235
switching default toolchains with rustup 234
compress() function 27 – 28
compressBound function 28
concurrency 157, 166 – 169
const generics 70
Context type alias 86
copies, avoiding 99 – 100
copy constructors 96
Copy trait 107
copy_from_slice() method 71
COUNT variable 130
Coveralls 139
Cow (clone on write) trait 107
crates.io 3
CreateTodo struct 193
cross compilation 30
CRUD (create, read, update, delete) 184
custom allocators 110 – 117
creating for protected memory 113 – 117
writing 110 – 113
D
data modeling 190 – 194
interfacing with data 191 – 194
SQL schema 190 – 191
data structures 65 – 66
error handling with Result enum 87 – 88
maps 76
Result enum 87 – 88
slices and arrays 69 – 72
strings 66 – 69
String vs. str 66 – 67
using effectively 67 – 69
vectors 74
types related to 74
Vec 73
wrapping 73 – 74
DATABASE_URL environment variable 189
deallocate() method 111, 113
Debug trait 76
debugging
async code 177 – 180
macros with cargo-expand 59 – 60
deep copying 97 – 98
dependency management 16 – 19
handling Cargo.lock file 18 – 19
Deref trait 72
DerefMut trait 72
#[derive] attribute 76
derive macro 207
#[derive(Clone)] attribute 98
destructures 70
doc attribute feature 58
doc compiler attribute 57
doc keyword 58
doc_cfg 57
doctest keyword 58
documentation, code examples in 34 – 35
doubly linked list 105
Drop trait 68
dryoc crate 19, 55, 86, 114
dyn Trait syntax 220
E
ecosystem, changing 140
embedded environments 41 – 42
memory allocation 42
summary 42
Embedded Rust Book, The 42
enums 86
Eq trait 76
Error enum 197
error handling 197 – 198
with Result enum 87 – 88
errors, handling gracefully 212
expect() function 123
expected result 148
external integration testing 146 – 150
libraries and tooling 147 – 150
assert_cmd 147 – 149
proptest 149
extractors 196
F
feature flags 19 – 21
FFI (foreign function interface) 27, 230
handling compatibility with Rust’s types 90 – 92
File:open function 88
fill_with() function 71
fill() function 71
finish() method 76
fixtures 148
for_loop() method 224
free() function 110 – 112
From implementation 88
From trait 84, 123, 193, 197, 212
“full” feature flag 181
Future trait 164, 170 – 171, 183
futures, handling async task results 161 – 164
#[tokio::main] 164
futures::future::join_all( ) function 167
fuzz subdirectory 151
fuzz testing 60 – 153
G
generics 3
GlobalAlloc API 110
GlobalAlloc trait 42
H
Hash trait 76
Hasher API 76
HashMap 74 – 76, 137
creating hashable types 76
custom hashing functions 75 – 76
HashSet 74
heapless crate 42
heaps 94 – 96
help command 207
Homebrew, installing tools using 233
HTTP REST API (application programming interface) CLI 204 – 205
deciding which tools and libraries to use 205 – 206
declaring commands 207 – 209
designing CLI 206 – 207
handling errors gracefully 212
implementing commands 209 – 210
requests 210 – 211
testing 212 – 214
HTTP REST API service
API design 184 – 185
application scaffolding 187 – 190
init_dbpool() function 189 – 190
init_tracing() function 188
main() function 187 – 188
building
data modeling 190 – 194
error handling 197 – 198
creating architecture 184 – 185
declaring API routes 194 – 195
implementing API routes 195 – 197
libraries and tools 185 – 187
running 198 – 202
web frameworks 183
HTTPie, installing 199, 235
Hyper 183
I
IDEs (integrated development environments)
integration with 56 – 58
nightly channel 57 – 58
nightly only features 57 – 58
using nightly on published crates 58
toolchains 57 – 58
stable vs. nightly 57 – 58
VS Code 56 – 58
impl keyword 83
init_dbpool() function 189 – 190
init_tracing() function 188
integration testing 141 – 153
external vs. built-in 146 – 150
libraries and tooling 147 – 150
fuzz testing 153
libraries and tooling 147 – 150
overview 144 – 146
testing strategies 144 – 146
interior mutability 105
Into trait 193
into_iter() method 223 – 224
isize identifier 79
iter() method 223
iterating
cargo-tree 61 – 62
cargo-watch 60 – 61
J
join() function 229
jq tool 206
K
Key type alias 86
L
languages, other, using Rust to accelerate 229 – 230
Layout struct 113
lazy_static! macro 132
libc crate 40
libc dependency 28
libFuzzer library 150
libraries and tools 185 – 187
libtest 129
link attribute 28
linked lists, smart pointers 100 – 103
LinkedList 74
linking to C libraries 27 – 29
lints 52 – 53
Linux, installing rustup on 233 – 234
LLDB (Low Level Debugger) 43
LLVM (Low Level Virtual Machine) 42 – 43
lowercased() function 100
M
macOS, installing tools using Homebrew 233
macros 3, 164, 180, 207
debugging with cargo-expand 59 – 60
magic completions 46 – 47
main() function 14, 40, 129, 143, 164, 168, 187 – 188, 212
make_mut() method 107
malloc() function 95, 110 – 112
maps 76
creating hashable types 76
custom hashing functions 75 – 76
McNamara, Tim 231
memcpy() function 71
memory
avoiding copies 99 – 100
clone on write 107 – 109
custom allocators 110 – 117
creating for protected memory 113 – 117
writing 110 – 113
deep copying 97 – 98
ownership 96 – 97
borrow checker 96 – 97
copy constructors 96
enforcing 96 – 97
memory allocation 42
memory management
heap and stack 94 – 96
reference counting 104 – 106
smart pointers 100 – 103
summarized 117 – 118
memset() function 71
mlock() function 114
mod keyword 35
mod statement 37
modules 35 – 37
monad 101
mprotect() function 114
mpsc channel 176
mut keyword 96
Mutex 105, 131
N
new keyword 95
new() method 101
nightly channel
nightly only features 57 – 58
using nightly on published crates 58
not() function 117
not(predicate) attribute 21
O
Observable trait 172 – 173
observe() method 170
Observer trait 171 – 172
observers, implementing in async Rust 169 – 174
oneshot channel 176
optimizations 219 – 232
SIMD (single instruction, multiple data) 226 – 227
using Rust to accelerate other languages 229 – 230
vectors 226
fast copies with Vec and slices 225 – 226
iterators 223 – 224
memory allocation 221 – 223
where to go from here 231
zero-cost abstractions 220
Option 101 – 103
overflowing_add() function 127
override option 16
ownership 96 – 97
P
panic!() macro 122
parallelism 158, 166 – 169
parallelization, with rayon 227 – 229
parameterized crate 125
parse() method 153
PartialEq trait 76
partition() function 142, 144
patching dependencies 21 – 24
best practices for 23 – 24
indirect dependencies 23
ping() function 195
poll() method 164, 171
posix_memalign() function 114
primitive types 77 – 80
arithmetic on primitives 79 – 80
integer types 77 – 78
size types 79
println completion 47
println!() function 165
project management
documenting Rust projects 31 – 35
code examples in documentation 34 – 35
linking to C libraries 27 – 29
with Cargo
custom building scripts 40 – 41
feature flags 19 – 21
proptest crate 125, 149
proptest! macro 126
protected memory 113 – 117
protected module 58
protoc-rust 40
ptr::copy_nonoverlapping() function 71
pub keyword 36
publishing crates 24 – 27
CI/CD integration 24 – 27
Q
QEMU project 42
QuickCheck 125
quicksort algorithm 142
quicksort() function 142, 145
R
RAII (resource acquisition is initialization) 4
rayon crate, parallelization with 227 – 229
read_to_string() function 88
realloc() method 111
redundant_clone lint 53
RefCell type 105
reference counting 104 – 106
references 96
#[repr(C)] attribute 90
request() function 210
requests 210 – 211
reqwest library 205
REST API service
application scaffolding 187 – 190
building, data modeling 190 – 194
declaring API routes 194 – 195
implementing API routes 195 – 197
Result 129
Result enum 87 – 88
rewriting 137 – 138
Rocket framework 183
Router 194
RowNotFound case 198
rstest crate 125
ruduino crate 42
runtime, with #[tokio::main] 164
Rust 1 – 8
documenting projects 31 – 35
overview 2 – 3
projects in embedded environments 41 – 42
memory allocation 42
summary 42
rust-analyzer 45 – 47
magic completions 46 – 47
tools 8
additional 58 – 62
unique features of 4 – 6
modern 5
open source 5 – 6
safety 4 – 5
vs. other popular languages 6
use cases 7 – 8
Rust Design Patterns (Matthews) 231
Rust in Action (McNamara) 231
Rust language website 6
rust-analyzer 3, 44 – 47, 135
magic completions 46 – 47
rust-bindgen tool 91
rust-clippy 3
rustc 3, 12
managing with rustup 234 – 235
installing HTTPie 235
installing rustc and other components 234
switching default toolchains with rustup 234
rustc compiler 4, 42 – 43, 51
rustc wrapper 55
RUSTC_WRAPPER argument 55
rustdoc attribute 58
rustdoc tool 31
RUSTFLAGS environment variable 31
rustfmt 3, 44, 49 – 51, 234
configuring 50 – 51
installing 50
rustup 56
installing on Linux- or UNIX-based systems 233 – 234
managing rustc and other Rust components with 234 – 235
installing HTTPie 235
installing rustc and other components 234
switching default toolchains with rustup 234
rustup toolchain install nightly command 16
RwLock 105, 131
S
safety, Rust 4 – 5
saturating_add() function 127
sccache tool 3, 44, 55 – 56
configuring 56
installing 55
scripts, custom building 40 – 41
select( ) system call 167
self parameter 99, 171
semantic versioning (SemVer) 17
semver crate 18
Send trait 104, 130 – 131
SIMD (single instruction, multiple data) 226 – 227
size property 113
size() method 112
sleep() function 162
slices 69 – 72
fast copies with Vec and 225 – 226
smart pointers 100 – 103
summarized 117 – 118
snippets 46
split_at() function 70
SQL schema 190 – 191
sqlx-cli tool 186
src directory 13
stack 95
stacks 94 – 96
'static lifetime specifier 67
static typing 3
std::collections::LinkedList 103
std::default::Default 75
std::hash::BuildHasher 75
std::hash::Hasher 75
std::io::Error type 88
std::simd module 226
std::span 69
std::string_view 69
std::sync::Once primitive 133
strings 66 – 69
String vs. str 66 – 67
using effectively 67 – 69
strndup() function 67
structs 83
structural search and replace tool 135
swap() function 81
sync module 176
sync Rust, mixing sync and async 174 – 176
Sync trait 104, 131
T
TDD (test driven development) 144
#[test] attribute 143
test-case crate 125
"test-util" feature 181
testing
fuzz testing 60
review of built-in testing features 124 – 125
unit testing
handling parallel test special cases and global state 129 – 134
what not to test 129 – 134
testing async code 180 – 181
testing frameworks 125 – 128
tfn completion 47
thiserror crate 216
time command 15
tmod completion 47
to_lowercase() function 100
Todo struct 191
#[tokio::main] 164
#[tokio::test] macro 180
tokio_test crate 181
TOKIO_WORKER_THREADS environment variable 188
tokio::join!( ) function 167
tokio::main macro 188
tokio::runtime::Handle 180
tokio::runtime::Handle::block_on() method 166
tokio::select!( ) macro 167
tokio::spawn( ) function 168
tokio::sync::mpsc module 176
tokio::task::spawn_blocking() function 166, 175
tokio::task::spawn() function 164 – 168
TOML (Tom’s obvious, minimal language) 13
too_many_arguments warning 53
toolchains 15
+channel option 15
stable vs. nightly 57 – 58
tooling 43 – 62
additional tools 58 – 62
cargo-expand 59 – 60
cargo-fuzz 60
cargo-tree 61 – 62
cargo-update 59
cargo-watch 60 – 61
Clippy 55
automatically applying suggestions 54
configuring 53 – 54
installing 51
lints 52 – 53
using in CI/CD 54 – 55
IDEs (integrated development environments)
integration with 56 – 58
nightly channel 57 – 58
toolchains 57 – 58
VS Code 56 – 58
overview of 44
rustfmt 49 – 51
configuring 50 – 51
installing 50
tooling: rustfmt (continued)
sccache tool 55 – 56
configuring 56
installing 55
tools 8
installing for this book 233 – 234
on Linux- or UNIX-based systems 233 – 234
on macOS using Homebrew 233
on Windows 234
refactoring 134 – 139
Tower library 183
#[tracing::instrument] macro 177
tracing async code 177 – 180
traits 3
try_new() method 101
tuple struct 82
tuples 80 – 81
types 77
enums 86
handling FFI compatibility with 90 – 92
primitive types 77 – 80
arithmetic on primitives 79 – 80
integer types 77 – 78
size types 79
structs 83
tuples 80 – 81
U
unchecked_add() function 127
uncompress() function 27 – 28
unit struct 82
unit testing 121 – 140
code coverage 139
dealing with changing ecosystem 140
differences in Rust 122 – 123
handling parallel test special cases and global state 129 – 134
refactoring 134 – 138
tools 134 – 138
review of built-in testing features 124 – 125
testing frameworks 125 – 128
what not to test 129 – 134
unix configuration predicate 117
UNIX-based systems, installing rustup on 233 – 234
unsafe keyword 67, 111, 113, 122
unwrap() function 129
update() method 172
UpdateTodo struct 193
use statements 35 – 36
usize identifier 79
UX (user experience) 144
V
Vec 73
Vec data structure 95
Vec::copy_from_slice() method 225
Vec::shrink_to_fit() method 223
VecDeque 74
vectors 74, 226
fast copies with Vec and slices 225 – 226
iterators 223 – 224
memory allocation 221 – 223
types related to 74
Vec 73
wrapping 73 – 74
VirtualAlloc() function 114
VirtualLock() function 114
VirtualProtect() function 114
VS Code (Visual Studio Code) 56 – 58
features of 56 – 58
integration with 56 – 58
vtables 220
W
Wasm (WebAssembly) 2
watch channel 176
web frameworks 183
windows configuration predicate 117
Windows Subsystem for Linux (WSL) 234
Windows, installing tools on 234
workspaces 38 – 39
Wrapping struct 127
wrapping_add() function 127
WrappingAdd trait 127 – 128
write() method 76
Y
yield points 163
Z
-Z unstable-options option 54
zero-cost abstractions 220
zlib library 27