Files
docs-rust/ch12/ch12-00-an-io-project.html
2026-06-22 21:27:36 +05:30

45 lines
3.0 KiB
HTML
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>An I/O Project: Building a Command Line Program</title>
</head>
<body>
<h1 id="an-io-project-building-a-command-line-program"><a class="header" href="#an-io-project-building-a-command-line-program">An I/O Project: Building a Command Line Program</a></h1>
<p>This chapter is a recap of the many skills youve learned so far and an
exploration of a few more standard library features. Well build a command line
tool that interacts with file and command line input/output to practice some of
the Rust concepts you now have under your belt.</p>
<p>Rusts speed, safety, single binary output, and cross-platform support make it
an ideal language for creating command line tools, so for our project, well
make our own version of the classic command line search tool <code>grep</code>
(<strong>g</strong>lobally search a <strong>r</strong>egular <strong>e</strong>xpression and <strong>p</strong>rint). In the
simplest use case, <code>grep</code> searches a specified file for a specified string. To
do so, <code>grep</code> takes as its arguments a file path and a string. Then, it reads
the file, finds lines in that file that contain the string argument, and prints
those lines.</p>
<p>Along the way, well show how to make our command line tool use the terminal
features that many other command line tools use. Well read the value of an
environment variable to allow the user to configure the behavior of our tool.
Well also print error messages to the standard error console stream (<code>stderr</code>)
instead of standard output (<code>stdout</code>) so that, for example, the user can
redirect successful output to a file while still seeing error messages onscreen.</p>
<p>One Rust community member, Andrew Gallant, has already created a fully
featured, very fast version of <code>grep</code>, called <code>ripgrep</code>. By comparison, our
version will be fairly simple, but this chapter will give you some of the
background knowledge you need to understand a real-world project such as
<code>ripgrep</code>.</p>
<p>Our <code>grep</code> project will combine a number of concepts youve learned so far:</p>
<ul>
<li>Organizing code (<a href="../ch07/ch07-00-managing-growing-projects-with-packages-crates-and-modules.html">Chapter 7</a><!-- ignore -->)</li>
<li>Using vectors and strings (<a href="../ch08/ch08-00-common-collections.html">Chapter 8</a><!-- ignore -->)</li>
<li>Handling errors (<a href="../ch09/ch09-00-error-handling.html">Chapter 9</a><!-- ignore -->)</li>
<li>Using traits and lifetimes where appropriate (<a href="../ch10/ch10-00-generics.html">Chapter 10</a><!-- ignore -->)</li>
<li>Writing tests (<a href="../ch11/ch11-00-testing.html">Chapter 11</a><!-- ignore -->)</li>
</ul>
<p>Well also briefly introduce closures, iterators, and trait objects, which
<a href="../ch13/ch13-00-functional-features.html">Chapter 13</a><!-- ignore --> and <a href="../ch18/ch18-00-oop.html">Chapter 18</a><!-- ignore --> will
cover in detail.</p>
</body>
</html>