]> git.proxmox.com Git - rustc.git/blob - src/doc/book/src/ch12-00-an-io-project.md
New upstream version 1.62.1+dfsg1
[rustc.git] / src / doc / book / src / ch12-00-an-io-project.md
1 # An I/O Project: Building a Command Line Program
2
3 This chapter is a recap of the many skills you’ve learned so far and an
4 exploration of a few more standard library features. We’ll build a command line
5 tool that interacts with file and command line input/output to practice some of
6 the Rust concepts you now have under your belt.
7
8 Rust’s speed, safety, single binary output, and cross-platform support make it
9 an ideal language for creating command line tools, so for our project, we’ll
10 make our own version of the classic command line search tool `grep`
11 (**g**lobally search a **r**egular **e**xpression and **p**rint). In the
12 simplest use case, `grep` searches a specified file for a specified string. To
13 do so, `grep` takes as its arguments a filename and a string. Then it reads the
14 file, finds lines in that file that contain the string argument, and prints
15 those lines.
16
17 Along the way, we’ll show how to make our command line tool use the terminal
18 features that many other command line tools use. We’ll read the value of an
19 environment variable to allow the user to configure the behavior of our tool.
20 We’ll also print error messages to the standard error console stream (`stderr`)
21 instead of standard output (`stdout`), so, for example, the user can redirect
22 successful output to a file while still seeing error messages onscreen.
23
24 One Rust community member, Andrew Gallant, has already created a fully
25 featured, very fast version of `grep`, called `ripgrep`. By comparison, our
26 version will be fairly simple, but this chapter will give you some of the
27 background knowledge you need to understand a real-world project such as
28 `ripgrep`.
29
30 Our `grep` project will combine a number of concepts you’ve learned so far:
31
32 * Organizing code (using what you learned about modules in [Chapter 7][ch7]<!--
33 ignore -->)
34 * Using vectors and strings (collections, [Chapter 8][ch8]<!-- ignore -->)
35 * Handling errors ([Chapter 9][ch9]<!-- ignore -->)
36 * Using traits and lifetimes where appropriate ([Chapter 10][ch10]<!-- ignore
37 -->)
38 * Writing tests ([Chapter 11][ch11]<!-- ignore -->)
39
40 We’ll also briefly introduce closures, iterators, and trait objects, which
41 Chapters [13][ch13]<!-- ignore --> and [17][ch17]<!-- ignore --> will cover in
42 detail.
43
44 [ch7]: ch07-00-managing-growing-projects-with-packages-crates-and-modules.html
45 [ch8]: ch08-00-common-collections.html
46 [ch9]: ch09-00-error-handling.html
47 [ch10]: ch10-00-generics.html
48 [ch11]: ch11-00-testing.html
49 [ch13]: ch13-00-functional-features.html
50 [ch17]: ch17-00-oop.html