以下内容来源于逆锋起笔
据说学 Rust 的人中,有不少是前端的,主要是很多前端工具链采用 Rust 重写。不仅如此,终端命令工具,也有很多 Rust 的轮子。所以,学学 Rust 造 command line 工具的轮子,对学习 Rust 挺好。
前几天发现有一本专门讲解 Rust 实现命令行工具的书,推荐给大家。
https://www.oreilly.com/library/view/command-line-rust/9781098109424/
这本书不是专注于整个语言,而是在每一章中使用一个小型、完整的程序来讲解 Rust。作者 Ken Youens-Clark 向你展示了如何启动、编写和测试每个程序以创建成品。你将学习如何处理 Rust 中的错误、读写文件以及使用正则表达式、Rust 类型、结构等等。
本书的目录如下:
1. Truth or Consequences
- Getting Started with “Hello, world!”
- Organizing a Rust Project Directory
- Creating and Running a Project with Cargo
- Writing and Running Integration Tests
- Adding a Project Dependency
- Understanding Program Exit Values
- Testing the Program Output
- Exit Values Make Programs Composable
- Summary
2. Test for Echo
- How echo Works
- Getting Started
- Accessing the Command-Line Arguments
- Adding clap as a Dependency
- Parsing Command-Line Arguments Using clap
- Creating the Program Output
- Writing Integration Tests
- Creating the Test Output Files
- Comparing Program Output
- Using the Result Type
- Summary
3. On the Catwalk
- How cat Works
- Getting Started
- Starting with Tests
- Creating a Library Crate
- Defining the Parameters
- Iterating Through the File Arguments
- Opening a File or STDIN
- Using the Test Suite
- Solution
- Reading the Lines in a File
- Printing Line Numbers
- Going Further
- Summary
4. Head Aches
- How head Works
- Getting Started
- Writing a Unit Test to Parse a String into a Number
- Converting Strings into Errors
- Defining the Arguments
- Processing the Input Files
- Reading Bytes Versus Characters
- Solution
- Reading a File Line by Line
- Preserving Line Endings While Reading a File
- Reading Bytes from a File
- Printing the File Separators
- Going Further
- Summary
5. Word to Your Mother
- How wc Works
- Getting Started
- Iterating the Files
- Writing and Testing a Function to Count File Elements
- Solution
- Counting the Elements of a File or STDIN
- Formatting the Output
- Going Further
- Summary
6. Den of Uniquity
- How uniq Works
- Getting Started
- Defining the Arguments
- Testing the Program
- Processing the Input Files
- Solution
- Going Further
- Summary
7. Finders Keepers
- How find Works
- Getting Started
- Defining the Arguments
- Validating the Arguments
- Finding All the Things
- Solution
- Conditionally Testing on Unix Versus Windows
- Going Further
- Summary
8. Shave and a Haircut
- How cut Works
- Getting Started
- Defining the Arguments
- Parsing the Position List
- Extracting Characters or Bytes
- Parsing Delimited Text Files
- Solution
- Selecting Characters from a String
- Selecting Bytes from a String
- Selecting Fields from a csv::StringRecord
- Final Boss
- Going Further
- Summary
9. Jack the Grepper
- How grep Works
- Getting Started
- Defining the Arguments
- Finding the Files to Search
- Finding the Matching Lines of Input
- Solution
- Going Further
- Summary
10. Boston Commons
- How comm Works
- Getting Started
- Defining the Arguments
- Validating and Opening the Input Files
- Processing the Files
- Solution
- Going Further
- Summary
11. Tailor Swyfte
- How tail Works
- Getting Started
- Defining the Arguments
- Parsing Positive and Negative Numeric Arguments
- Using a Regular Expression to Match an Integer with an Optional Sign
- Parsing and Validating the Command-Line Arguments
- Processing the Files
- Counting the Total Lines and Bytes in a File
- Finding the Starting Line to Print
- Finding the Starting Byte to Print
- Testing the Program with Large Input Files
- Solution
- Counting All the Lines and Bytes in a File
- Finding the Start Index
- Printing the Lines
- Printing the Bytes
- Benchmarking the Solution
- Going Further
- Summary
12. Fortunate Son
- How fortune Works
- Getting Started
- Defining the Arguments
- Finding the Input Sources
- Reading the Fortune Files
- Randomly Selecting a Fortune
- Printing Records Matching a Pattern
- Solution
- Going Further
- Summary
13. Rascalry
- How cal Works
- Getting Started
- Defining and Validating the Arguments
- Writing the Program
- Solution
- Going Further
- Summary
14. Elless Island
- How ls Works
- Getting Started
- Defining the Arguments
- Finding the Files
- Formatting the Long Listing
- Displaying Octal Permissions
- Testing the Long Format
- Solution
- Notes from the Testing Underground
- Going Further
- Summary
Epilogue
Index
About the Author