Title Page....2
Copyright....-1
The Complete Rust Programming Reference Guide....4
About Packt....6
Why subscribe?....7
Packt.com....8
Contributors....9
About the authors....10
Packt is searching for authors like you....11
Preface....27
Who this book is for....28
What this book covers....29
To get the most out of this book....32
Download the example code files....33
Conventions used....34
Get in touch....35
Reviews....36
Getting Started with Rust....37
What is Rust and why should you care?....38
Installing the Rust compiler and toolchain....46
Using rustup.rs....47
A tour of the language....50
Primitive types....54
Declaring variables and immutability....56
Functions....58
Closures....60
Strings....62
Conditionals and decision making....63
Match expressions....65
Loops....67
User-defined types....70
Structs....71
Enums....74
Functions and methods on types....77
Impl blocks on structs....78
Impl blocks for enums....81
Modules, imports, and use statements....83
Collections....84
Arrays....85
Tuples....86
Vectors....87
Hashmaps....88
Slices....89
Iterators....91
Exercise – fixing the word counter....93
Summary....95
Managing Projects with Cargo....96
Package managers....97
Modules....98
Nested modules....99
File as a module....102
Directory as module....104
Cargo and crates....107
Creating a new Cargo project....109
Cargo and dependencies....112
Running tests with Cargo....115
Running examples with Cargo....119
Cargo workspace....120
Extending Cargo and tools....122
Subcommands and Cargo installation....123
cargo-watch....124
cargo-edit....125
cargo-deb....126
cargo-outdated....127
Linting code with clippy....128
Exploring the manifest file – Cargo.toml....130
Setting up a Rust development environment....134
Building a project with Cargo – imgtool....139
Summary....143
Tests, Documentation, and Benchmarks....144
Motivation for testing....145
Organizing tests....147
Testing primitives....148
Attributes....149
Assertion macros....150
Unit tests....152
First unit test....153
Running tests....154
Isolating test code....155
Failing tests....157
Ignoring tests....158
Integration tests....160
First integration test....161
Sharing common code....163
Documentation....165
Writing documentation....166
Generating and viewing documentation....167
Hosting documentation....168
Doc attributes....169
Documentation tests....170
Benchmarks....172
Built-in micro-benchmark harness....173
Benchmarking on stable Rust....176
Writing and testing a crate – logic gate simulator....180
Continuous integration with Travis CI....185
Summary....190
Types, Generics, and Traits....191
Type systems and why they matter....192
Generics....194
Creating generic types....196
Generic functions....197
Generic types....199
Generic implementations....200
Using generics....202
Abstracting behavior with traits....204
Traits....205
The many forms of traits....212
Marker traits....213
Simple traits....214
Generic traits....215
Associated type traits....216
Inherited traits....217
Using traits with generics – trait bounds....218
Trait bounds on types....223
Trait bounds on generic functions and impl blocks....224
Using + to compose traits as bounds....225
Trait bounds with impl trait syntax....227
Exploring standard library traits....229
True polymorphism using trait objects....237
Dispatch....238
Trait objects....239
Summary....242
Memory Management and Safety....243
Programs and memory....245
How do programs use memory?....248
Memory management and its kinds....250
Approaches to memory allocation....252
The stack....253
The heap....255
Memory management pitfalls....258
Memory safety....259
Trifecta of memory safety....262
Ownership....263
A brief on scopes....266
Move and copy semantics....268
Duplicating types via traits....271
Copy....272
Clone....273
Ownership in action....275
Borrowing....280
Borrowing rules....283
Borrowing in action....284
Method types based on borrowing....287
Lifetimes....288
Lifetime parameters....291
Lifetime elision and the rules....292
Lifetimes in user defined types....294
Lifetime in impl blocks....295
Multiple lifetimes....296
Lifetime subtyping....297
Specifying lifetime bounds on generic types....298
Pointer types in Rust....300
References – safe pointers....301
Raw pointers....302
Smart pointers....303
Drop....304
Deref and DerefMut....306
Types of smart pointers....307
Box<T>....308
Reference counted smart pointers....311
Rc<T>....312
Interior mutability....318
Cell<T>....319
RefCell<T>....321
Uses of interior mutability....323
Summary....325
Error Handling....326
Error handling prelude....327
Recoverable errors....330
Option....331
Result....336
Combinators on Option/Result....341
Common combinators....342
Using combinators....344
Converting between Option and Result....346
Early returns and the ? operator....347
Non-recoverable errors....349
User-friendly panics....354
Custom errors and the Error trait....355
Summary....360
Advanced Concepts....361
Type system tidbits....362
Blocks and expressions....363
Let statements....367
Loop as an expression....373
Type clarity and sign distinction in numeric types....374
Type inference....377
Type aliases....379
Strings....381
Owned strings – String....382
Borrowed strings – &str....385
Slicing and dicing strings....388
Using strings in functions....390
Joining strings....391
When to use &str versus String ?....393
Global values....394
Constants....395
Statics....396
Compile time functions – const fn....397
Dynamic statics using the lazy_static! macro....399
Iterators....400
Implementing a custom iterator....402
Advanced types....406
Unsized types....407
Function types....409
Never type ! and diverging functions....410
Unions....411
Cow....413
Advanced traits....414
Sized and ?Sized....415
Borrow and AsRef....416
ToOwned....417
From and Into....418
Trait objects and object safety....419
Universal function call syntax....421
Trait rules....422
Closures in depth....423
Fn closures....424
FnMut closures....425
FnOnce closures....426
Consts in structs, enums, and traits....427
Modules, paths, and imports....429
Imports....430
Re-exports....431
Selective privacy....433
Advanced match patterns and guards....434
Match guards....435
Advanced let destructure....436
Casting and coercion....437
Types and memory....438
Memory alignment....439
Exploring the std::mem module....440
Serialization and deserialization using serde....442
Summary....444
Concurrency....445
Program execution models....446
Concurrency....448
Approaches to concurrency....450
Kernel-based....451
User-level....453
Pitfalls....455
Concurrency in Rust....458
Thread basics....459
Customizing threads....462
Accessing data from threads....464
Concurrency models with threads....467
Shared state model....468
Shared ownership with Arc....470
Mutating shared data from threads....471
Mutex....473
Shared mutability with Arc and Mutex....474
RwLock....476
Communicating through message passing....477
Asynchronous channels....478
Synchronous channels....480
thread-safety in Rust....481
What is thread-safety?....482
Traits for thread-safety....484
Send....485
Sync....486
Concurrency using the actor model....487
Other crates....490
Summary....491
Metaprogramming with Macros....492
What is metaprogramming?....493
When to use and not use Rust macros....497
Macros in Rust and their types....498
Types of macros....502
Creating your first macro with macro_rules!....503
Built-in macros in the standard library....508
macro_rules! token types....510
Repetitions in macros....514
A more involved macro – writing a DSL for HashMap initialization....516
Macro use case – writing tests....518
Exercises....521
Procedural macros....522
Derive macros....524
Debugging macros....531
Useful procedural macro crates....533
Summary....534
Unsafe Rust and Foreign Function Interfaces....535
What is safe and unsafe really?....536
Unsafe functions and blocks....541
Unsafe traits and implementations....545
Calling C code from Rust....548
Calling Rust code from C....552
Using external C/C++ libraries from Rust....555
Creating native Python extensions with PyO3....559
Creating native extensions in Rust for Node.js....563
Summary....568
Logging....569
What is logging and why do we need it?....570
The need for logging frameworks....573
Logging frameworks and their key features....574
Approaches to logging....577
Unstructured logging....578
Structured logging....579
Logging in Rust....580
log – Rust's logging facade....581
The env_logger....583
log4rs....585
Structured logging using slog....588
Summary....594
Network Programming in Rust....595
Network programming prelude....596
Synchronous network I/O....600
Building a synchronous redis server....602
Asynchronous network I/O....609
Async abstractions in Rust....611
Mio....612
Futures....613
Tokio....614
Building an asynchronous redis server....615
Summary....620
Building Web Applications with Rust....621
Web applications in Rust....622
Typed HTTP with Hyper....624
Hyper server APIs – building a URL shortener ....626
hyper as a client – building a URL shortener client....630
Web frameworks....633
Actix-web basics....634
Building a bookmarks API using Actix-web....635
Summary....645
Lists, Lists, and More Lists....646
Linked lists....647
A transaction log....648
Adding entries....651
Log replay....653
After use....655
Wrap up....656
Upsides....657
Downsides....658
Doubly linked list....659
A better transaction log....661
Examining the log....662
Reverse....664
Wrap up....665
Upsides....667
Downsides....668
Skip lists....669
The best transaction log....672
The list....673
Adding data....674
Leveling up....676
Jumping around....678
Thoughts and discussion....680
Upsides....683
Downsides....684
Dynamic arrays....685
Favorite transactions....688
Internal arrays....689
Quick access....691
Wrap up....693
Upsides....694
Downsides....695
Summary....696
Further reading....698
Robust Trees....699
Binary search tree....700
IoT device management....702
More devices....703
Finding the right one....705
Finding all devices....706
Wrap up....708
Upsides....709
Downsides....710
Red-black tree....711
Better IoT device management....717
Even more devices....718
Balancing the tree....720
Finding the right one, now....723
Wrap up....725
Upsides....726
Downsides....727
Heaps....728
A huge inbox....730
Getting messages in....731
Taking messages out....732
Wrap up....733
Upsides....734
Downsides....735
Trie....736
More realistic IoT device management....738
Adding paths....739
Walking....740
Wrap up....741
Upsides....742
Downsides....743
B-Tree....744
An IoT database....747
Adding stuff....749
Searching for stuff....752
Walking the tree....754
Wrap up....755
Upsides....758
Downsides....759
Graphs....760
The literal Internet of Things....765
Neighborhood search....767
The shortest path....769
Wrap up....774
Upsides....775
Downsides....776
Summary....777
Exploring Maps and Sets....779
Hashing....780
Create your own....783
Message digestion....786
Wrap up....788
Maps....792
A location cache....795
The hash function....796
Adding locations....797
Fetching locations....798
Wrap up....799
Upsides....801
Downsides....802
Sets....803
Storing network addresses....806
Networked operations....808
Union....810
Intersection....811
Difference....812
Wrap up....813
Upsides....814
Downsides....815
Summary....816
Further reading....818
Collections in Rust....819
Sequences....820
Vec<T> and VecDeque<T>....821
Architecture....822
Insert....824
Look up....826
Remove....828
LinkedList<T>....829
Architecture....830
Insert....831
Look up....833
Remove....835
Wrap up....836
Maps and sets....837
HashMap and HashSet....838
Architecture....839
Insert....841
Lookup....843
Remove....844
BTreeMap and BTreeSet....845
Architecture....846
Insert....847
Look up....849
Remove....850
Wrap up....851
Summary....853
Further reading....854
Algorithm Evaluation....855
The Big O notation....856
Other people's code....857
The Big O....858
Asymptotic runtime complexity....859
Making your own....860
Loops....861
Recursion....862
Complexity classes....863
O(1)....864
O(log(n))....865
O(n)....866
O(n log(n))....867
O(n²)....868
O(2n)....869
Comparison....870
In the wild....871
Data structures....872
Everyday things....873
Exotic things....874
Summary....875
Further reading....876
Ordering Things....877
From chaos to order....878
Bubble sort....880
Shell sort....885
Heap sort....889
Merge sort....893
Quicksort....897
Summary....903
Further reading....905
Finding Stuff....906
Finding the best....907
Linear searches....908
Jump search....909
Binary searching....911
Wrap up....914
Summary....917
Further reading....918
Random and Combinatorial....919
Pseudo-random numbers....920
LCG....926
Wichmann-Hill....930
The rand crate....934
Back to front....935
Packing bags or the 0-1 knapsack problem....936
N queens....939
Advanced problem solving....944
Dynamic programming....945
The knapsack problem improved....946
Metaheuristic approaches....951
Example metaheuristic – genetic algorithms....952
Summary....955
Further reading....956
Algorithms of the Standard Library....957
Slicing and iteration....958
Iterator....959
Slices....961
Search....962
Linear search....963
Binary search....964
Sorting....966
Stable sorting....967
Unstable sorting....971
Summary....974
Further reading....975
Other Books You May Enjoy....976
Leave a review - let other readers know what you think....978
Rust is a powerful language with a rare combination of safety, speed, and zero-cost abstractions. This Learning Path is filled with clear and simple explanations of its features along with real-world examples, demonstrating how you can build robust, scalable, and reliable programs.
You'll get started with an introduction to Rust data structures, algorithms, and essential language constructs. Next, you will understand how to store data using linked lists, arrays, stacks, and queues. You'll also learn to implement sorting and searching algorithms, such as Brute Force algorithms, Greedy algorithms, Dynamic Programming, and Backtracking. As you progress, you'll pick up on using Rust for systems programming, network programming, and the web. You'll then move on to discover a variety of techniques, right from writing memory-safe code, to building idiomatic Rust libraries, and even advanced macros.
By the end of this Learning Path, you'll be able to implement Rust for enterprise projects, writing better tests and documentation, designing for performance, and creating idiomatic Rust code.
If you are already familiar with an imperative language and now want to progress from being a beginner to an intermediate-level Rust programmer, this Learning Path is for you. Developers who are already familiar with Rust and want to delve deeper into the essential data structures and algorithms in Rust will also find this Learning Path useful.