]> git.proxmox.com Git - rustc.git/blame - 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
CommitLineData
13cf67c4
XL
1# An I/O Project: Building a Command Line Program
2
3This chapter is a recap of the many skills you’ve learned so far and an
4exploration of a few more standard library features. We’ll build a command line
5tool that interacts with file and command line input/output to practice some of
6the Rust concepts you now have under your belt.
7
8Rust’s speed, safety, single binary output, and cross-platform support make it
9an ideal language for creating command line tools, so for our project, we’ll
04454e1e
FG
10make 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
12simplest use case, `grep` searches a specified file for a specified string. To
13do so, `grep` takes as its arguments a filename and a string. Then it reads the
14file, finds lines in that file that contain the string argument, and prints
15those lines.
16
17Along the way, we’ll show how to make our command line tool use the terminal
18features that many other command line tools use. We’ll read the value of an
13cf67c4 19environment variable to allow the user to configure the behavior of our tool.
532ac7d7
XL
20We’ll also print error messages to the standard error console stream (`stderr`)
21instead of standard output (`stdout`), so, for example, the user can redirect
22successful output to a file while still seeing error messages onscreen.
13cf67c4
XL
23
24One Rust community member, Andrew Gallant, has already created a fully
25featured, very fast version of `grep`, called `ripgrep`. By comparison, our
04454e1e
FG
26version will be fairly simple, but this chapter will give you some of the
27background knowledge you need to understand a real-world project such as
13cf67c4
XL
28`ripgrep`.
29
30Our `grep` project will combine a number of concepts you’ve learned so far:
31
48663c56
XL
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 -->)
13cf67c4
XL
39
40We’ll also briefly introduce closures, iterators, and trait objects, which
48663c56
XL
41Chapters [13][ch13]<!-- ignore --> and [17][ch17]<!-- ignore --> will cover in
42detail.
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