1
1
mirror of https://github.com/theoludwig/rust_book.git synced 2024-07-17 08:30:11 +02:00
rust_book/chapter_12_minigrep
2024-02-08 11:21:23 +01:00
..
src chore: initial commit 2023-12-06 19:18:03 +01:00
tests chore: initial commit 2023-12-06 19:18:03 +01:00
Cargo.lock chore: initial commit 2023-12-06 19:18:03 +01:00
Cargo.toml chore: initial commit 2023-12-06 19:18:03 +01:00
poem.txt chore: initial commit 2023-12-06 19:18:03 +01:00
README.md docs: add README for every chapters 2024-02-08 11:21:23 +01:00

12. An I/O Project: Building a Command Line Program: minigrep

Command line tool that interacts with file and command line input/output to practice some of the Rust concepts we've learned so far.

Rust's speed, safety, single binary output, and cross-platform support make it an ideal language for creating command line tools, so for our project, we'll make our own version of the classic command line search tool grep (globally search a regular expression and print).

grep 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 only those lines (containing the string).

We'll call our project minigrep to distinguish it from the grep tool that you might already have on your system.

The first task is to make minigrep accept its two command line arguments: the file path and a string to search for.

Example usage

cargo test

It searches for the string "the" in the file poem.txt and prints all the lines that included the string to the terminal:

cargo run -- "the" "poem.txt"
# or
IGNORE_CASE="false" cargo run -- "the" "poem.txt"

By default IGNORE_CASE is false and the search is case-sensitive.

similar to:

grep "the" "poem.txt"
IGNORE_CASE="true" cargo run -- "the" "poem.txt"

similar to:

grep -i "the" "poem.txt"