Preface....5
What Is Rust?....5
Who Should Read This Book....8
Why You Should Learn Rust....9
The Coding Challenges....10
Getting Rust and the Code....11
Conventions Used in This Book....14
Using Code Examples....15
O’Reilly Online Learning....16
How to Contact Us....16
Acknowledgments....17
1. Echo Canyon....18
Starting a New Binary Program with Cargo....19
How echo Works....22
Getting Command-Line Arguments....25
Adding a Project Dependency....29
Parsing Command-Line Arguments Using clap....31
Creating the Program Output....36
Writing and Running Integration Tests....40
Creating the Test Output Files....44
Comparing Program Output....45
Using the Result Type....47
Summary....50
2. On The Catwalk....53
How cat Works....54
Getting Started with Test-Driven Development....58
Code Organization....60
Defining the Parameters....61
Processing the Files....70
Solution....73
Reading the Lines in a File....73
Printing Line Numbers....75
Reading Standard In....78
Going Further....80
Summary....80
3. Head Aches....82
How head Works....83
Getting Started....86
Defining and Validating the Arguments....91
Parsing Strings into Numbers....92
Converting Strings into Errors....98
Writing the run() Function....102
Solution....105
Reading a File Line-by-line....105
Preserving Line Endings While Reading a File....106
Reading Bytes and Characters....109
Printing the File Separators....112
Going Further....114
Summary....114
4. The Tailor....116
How tail Works....117
Getting Started....122
Defining the Arguments....122
Reading the End of a File....129
Solution....130
Going Further....139
Summary....139
5. Elless Island....141
How ls Works....142
Getting Started....145
Defining the Arguments....146
Writing the Program....148
Displaying Octal Permissions....154
Solution....160
Testing....168
Going Further....173
Summary....174
6. Word To Your Mother....175
How wc Works....176
Getting Started....181
Iterating the Files....189
Solution....190
Counting the Elements of a File or STDIN....190
Formatting the Output....197
Going Further....203
Summary....204
For several consecutive years, Rust has been voted "most loved programming language" in Stack Overflow's annual developer survey. This open source systems programming language is now used for everything from game engines and operating systems to browser components and virtual reality simulation engines. But Rust is also an incredibly complex language with a notoriously difficult learning curve.
Rather than focus on the language as a whole, this guide teaches Rust using a single small, complete, focused program in each chapter. Author Ken Youens-Clark shows you how to start, write, and test each of these programs to create a finished product. You'll learn how to handle errors in Rust, read and write files, and use regular expressions, Rust types, structs, and more.