Filename: poem.txt

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

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

    Filename: src/main.rs

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

    First, we add some more use statements to bring in relevant parts of the standard library: we need std::fs::File to handle files, and std::io::prelude::* contains various useful traits for doing I/O, including file I/O. In the same way that Rust has a general prelude that brings certain types and functions into scope automatically, the module has its own prelude of common types and functions you’ll need when working with I/O. Unlike with the default prelude, we must explicitly add a use statement for the prelude from std::io.

    After those lines, 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: