Extra Meal Write a Simple Grep Command for the Midterm Test

Extra Lesson: Midterm Test - Implement a Simple grep Command Line #

Hello, I am Chen Tian.

Now that you have completed the basics of Rust, I believe you have the confidence to tackle some simple development tasks. Today, we’re going to have a midterm test to assess your understanding of Rust and your ability to apply what you’ve learned.

We’re going to build a small tool called rgrep, which is similar to the grep tool. If you’re a *nix user, you’ve likely used text searching tools like grep or ag.

The grep command is used to search for strings within files that meet a specified condition. If the content of a file matches the specified string, the grep command will display the line containing that string; if no file name is specified, or the given file name is -, the grep command will read data from the standard input device.

Our rgrep will be a bit simpler; it will support the following three usage scenarios:

First is the simplest, given a string and a file, print all the lines in the file that contain the string:

$ rgrep Hello a.txt
55: Hello world. This is an exmaple text

Then, we loosen the restriction, allowing the user to provide a regular expression to find all lines in the file containing that pattern:

$ rgrep Hel[^\\s]+ a.txt
55: Hello world. This is an exmaple text
89: Help me! I need assistant!

If this is also achievable, we’ll further loosen the restriction, allowing the user to provide a regular expression to search through all files that fit a file wildcard pattern (you can use globset or glob to handle the wildcards), for example:

$ rgrep Hel[^\\s]+ a*.txt
a.txt:
    55:1 Hello world. This is an exmaple text
    89:1 Help me! I need assistant!
    5:6  Use `Help` to get help.
abc.txt:
    100:1 Hello Tyr!

Where, the number before the colon is the line number, and the number after the colon is the character position in that line.

Here are some tips for you.

  • For the command line part, you can use clap3 or structopt, or you can just use env.args().
  • For regular expression support, you can utilize regex.
  • For file reading, you can rely on std::fs or tokio::fs. You can process all the files that fit the wildcard pattern sequentially, or you can use rayon or tokio for parallel processing.
  • For the display of results, it is best if you can highlight the matched text in a different color.

" alt="" />

If you have the energy to spare, you could take a look at the grep documentation and try to implement more features.

Good luck!

Keep it up, and I’ll see you in the next lesson for the homework review.