Reading a File

Now we’ll add functionality to read the file that is specified in the filename command line argument. First, we need a sample file to test it with: the best kind of file to use to make sure minigrep is working is one with a small amount of text over multiple lines with some repeated words. Listing 12-3 has an Emily Dickinson poem that will work well! Create a file called poem.txt at the root level of your project, and enter the poem “I’m Nobody! Who are you?”

Filename: poem.txt

  1. I'm nobody! Who are you?
  2. Are you nobody, too?
  3. Then there's a pair of us - don't tell!
  4. They'd banish us, you know.
  5. How dreary to be somebody!
  6. How public, like a frog
  7. To tell your name the livelong day
  8. To an admiring bog!

Listing 12-3: A poem by Emily Dickinson makes a good test case

With the text in place, edit src/main.rs and add code to read the file, as shown in Listing 12-4.

Filename: src/main.rs

  1. use std::env;
  2. use std::fs;
  3. fn main() {
  4. // --snip--
  5. let args: Vec<String> = env::args().collect();
  6. let query = &args[1];
  7. let filename = &args[2];
  8. println!("Searching for {}", query);
  9. println!("In file {}", filename);
  10. let contents = fs::read_to_string(filename)
  11. .expect("Something went wrong reading the file");
  12. println!("With text:\n{}", contents);
  13. }

Listing 12-4: Reading the contents of the file specified by the second argument

First, we add another use statement to bring in a relevant part of the standard library: we need std::fs to handle files.

In main, we’ve added a new statement: fs::read_to_string takes the filename, opens that file, and returns a Result<String> of the file’s contents.

After that statement, we’ve again added a temporary println! statement that prints the value of contents after the file is read, so we can check that the program is working so far.

Let’s run this code with any string as the first command line argument (because we haven’t implemented the searching part yet) and the poem.txt file as the second argument:

  1. $ cargo run the poem.txt
  2. Compiling minigrep v0.1.0 (file:///projects/minigrep)
  3. Finished dev [unoptimized + debuginfo] target(s) in 0.0s
  4. Running `target/debug/minigrep the poem.txt`
  5. Searching for the
  6. In file poem.txt
  7. With text:
  8. I'm nobody! Who are you?
  9. Are you nobody, too?
  10. Then there's a pair of us - don't tell!
  11. They'd banish us, you know.
  12. How dreary to be somebody!
  13. How public, like a frog
  14. To tell your name the livelong day
  15. To an admiring bog!

Great! The code read and then printed the contents of the file. But the code has a few flaws. The main function has multiple responsibilities: generally, functions are clearer and easier to maintain if each function is responsible for only one idea. The other problem is that we’re not handling errors as well as we could. The program is still small, so these flaws aren’t a big problem, but as the program grows, it will be harder to fix them cleanly. It’s good practice to begin refactoring early on when developing a program, because it’s much easier to refactor smaller amounts of code. We’ll do that next.