This issue also ties into the second problem: although query and file_path are configuration variables to our program, variables like contents are used to perform the program’s logic. The longer main becomes, the more variables we’ll need to bring into scope; the more variables we have in scope, the harder it will be to keep track of the purpose of each. It’s best to group the configuration variables into one structure to make their purpose clear.

    The third problem is that we’ve used expect to print an error message when reading the file fails, but the error message just prints Should have been able to read the file. Reading a file can fail in a number of ways: for example, the file could be missing, or we might not have permission to open it. Right now, regardless of the situation, we’d print the same error message for everything, which wouldn’t give the user any information!

    Fourth, we use expect repeatedly to handle different errors, and if the user runs our program without specifying enough arguments, they’ll get an index out of bounds error from Rust that doesn’t clearly explain the problem. It would be best if all the error-handling code were in one place so future maintainers had only one place to consult the code if the error-handling logic needed to change. Having all the error-handling code in one place will also ensure that we’re printing messages that will be meaningful to our end users.

    Let’s address these four problems by refactoring our project.

    The organizational problem of allocating responsibility for multiple tasks to the main function is common to many binary projects. As a result, the Rust community has developed guidelines for splitting the separate concerns of a binary program when main starts getting large. This process has the following steps:

    • Split your program into a main.rs and a lib.rs and move your program’s logic to lib.rs.
    • As long as your command line parsing logic is small, it can remain in main.rs.
    • When the command line parsing logic starts getting complicated, extract it from main.rs and move it to lib.rs.

    The responsibilities that remain in the main function after this process should be limited to the following:

    • Calling the command line parsing logic with the argument values
    • Setting up any other configuration
    • Calling a run function in lib.rs
    • Handling the error if run returns an error

    This pattern is about separating concerns: main.rs handles running the program, and lib.rs handles all the logic of the task at hand. Because you can’t test the main function directly, this structure lets you test all of your program’s logic by moving it into functions in lib.rs. The code that remains in main.rs will be small enough to verify its correctness by reading it. Let’s rework our program by following this process.

    Extracting the Argument Parser

    We’ll extract the functionality for parsing arguments into a function that main will call to prepare for moving the command line parsing logic to src/lib.rs. Listing 12-5 shows the new start of main that calls a new function parse_config, which we’ll define in src/main.rs for the moment.

    Filename: src/main.rs

    Listing 12-5: Extracting a parse_config function from main

    We’re still collecting the command line arguments into a vector, but instead of assigning the argument value at index 1 to the variable query and the argument value at index 2 to the variable file_path within the main function, we pass the whole vector to the parse_config function. The parse_config function then holds the logic that determines which argument goes in which variable and passes the values back to main. We still create the query and file_path variables in main, but main no longer has the responsibility of determining how the command line arguments and variables correspond.

    This rework may seem like overkill for our small program, but we’re refactoring in small, incremental steps. After making this change, run the program again to verify that the argument parsing still works. It’s good to check your progress often, to help identify the cause of problems when they occur.

    Grouping Configuration Values

    We can take another small step to improve the parse_config function further. At the moment, we’re returning a tuple, but then we immediately break that tuple into individual parts again. This is a sign that perhaps we don’t have the right abstraction yet.

    Another indicator that shows there’s room for improvement is the config part of parse_config, which implies that the two values we return are related and are both part of one configuration value. We’re not currently conveying this meaning in the structure of the data other than by grouping the two values into a tuple; we’ll instead put the two values into one struct and give each of the struct fields a meaningful name. Doing so will make it easier for future maintainers of this code to understand how the different values relate to each other and what their purpose is.

    Listing 12-6 shows the improvements to the parse_config function.

    Filename: src/main.rs

    1. use std::env;
    2. use std::fs;
    3. fn main() {
    4. let args: Vec<String> = env::args().collect();
    5. let config = parse_config(&args);
    6. println!("Searching for {}", config.query);
    7. println!("In file {}", config.file_path);
    8. let contents = fs::read_to_string(config.file_path)
    9. .expect("Should have been able to read the file");
    10. // --snip--
    11. println!("With text:\n{contents}");
    12. }
    13. struct Config {
    14. query: String,
    15. file_path: String,
    16. }
    17. fn parse_config(args: &[String]) -> Config {
    18. let query = args[1].clone();
    19. let file_path = args[2].clone();
    20. Config { query, file_path }
    21. }

    Listing 12-6: Refactoring parse_config to return an instance of a Config struct

    We’ve added a struct named Config defined to have fields named query and file_path. The signature of parse_config now indicates that it returns a Config value. In the body of parse_config, where we used to return string slices that reference String values in args, we now define Config to contain owned String values. The args variable in main is the owner of the argument values and is only letting the parse_config function borrow them, which means we’d violate Rust’s borrowing rules if Config tried to take ownership of the values in args.

    There are a number of ways we could manage the String data; the easiest, though somewhat inefficient, route is to call the clone method on the values. This will make a full copy of the data for the Config instance to own, which takes more time and memory than storing a reference to the string data. However, cloning the data also makes our code very straightforward because we don’t have to manage the lifetimes of the references; in this circumstance, giving up a little performance to gain simplicity is a worthwhile trade-off.

    We’ve updated main so it places the instance of Config returned by parse_config into a variable named config, and we updated the code that previously used the separate query and file_path variables so it now uses the fields on the Config struct instead.

    Now our code more clearly conveys that query and file_path are related and that their purpose is to configure how the program will work. Any code that uses these values knows to find them in the config instance in the fields named for their purpose.

    Creating a Constructor for Config

    So far, we’ve extracted the logic responsible for parsing the command line arguments from main and placed it in the parse_config function. Doing so helped us to see that the query and file_path values were related and that relationship should be conveyed in our code. We then added a Config struct to name the related purpose of query and file_path and to be able to return the values’ names as struct field names from the parse_config function.

    So now that the purpose of the parse_config function is to create a Config instance, we can change parse_config from a plain function to a function named new that is associated with the Config struct. Making this change will make the code more idiomatic. We can create instances of types in the standard library, such as String, by calling String::new. Similarly, by changing parse_config into a new function associated with Config, we’ll be able to create instances of Config by calling Config::new. Listing 12-7 shows the changes we need to make.

    Filename: src/main.rs

    1. use std::env;
    2. use std::fs;
    3. fn main() {
    4. let args: Vec<String> = env::args().collect();
    5. let config = Config::new(&args);
    6. println!("Searching for {}", config.query);
    7. println!("In file {}", config.file_path);
    8. let contents = fs::read_to_string(config.file_path)
    9. .expect("Should have been able to read the file");
    10. println!("With text:\n{contents}");
    11. // --snip--
    12. }
    13. // --snip--
    14. struct Config {
    15. query: String,
    16. file_path: String,
    17. }
    18. impl Config {
    19. fn new(args: &[String]) -> Config {
    20. let query = args[1].clone();
    21. let file_path = args[2].clone();
    22. Config { query, file_path }
    23. }
    24. }

    We’ve updated main where we were calling parse_config to instead call Config::new. We’ve changed the name of parse_config to new and moved it within an impl block, which associates the new function with Config. Try compiling this code again to make sure it works.

    Now we’ll work on fixing our error handling. Recall that attempting to access the values in the args vector at index 1 or index 2 will cause the program to panic if the vector contains fewer than three items. Try running the program without any arguments; it will look like this:

    1. $ cargo run
    2. Compiling minigrep v0.1.0 (file:///projects/minigrep)
    3. Finished dev [unoptimized + debuginfo] target(s) in 0.0s
    4. Running `target/debug/minigrep`
    5. thread 'main' panicked at 'index out of bounds: the len is 1 but the index is 1', src/main.rs:27:21
    6. note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

    The line index out of bounds: the len is 1 but the index is 1 is an error message intended for programmers. It won’t help our end users understand what they should do instead. Let’s fix that now.

    In Listing 12-8, we add a check in the new function that will verify that the slice is long enough before accessing index 1 and 2. If the slice isn’t long enough, the program panics and displays a better error message.

    Filename: src/main.rs

    1. use std::env;
    2. use std::fs;
    3. fn main() {
    4. let args: Vec<String> = env::args().collect();
    5. let config = Config::new(&args);
    6. println!("Searching for {}", config.query);
    7. println!("In file {}", config.file_path);
    8. let contents = fs::read_to_string(config.file_path)
    9. .expect("Should have been able to read the file");
    10. println!("With text:\n{contents}");
    11. }
    12. struct Config {
    13. query: String,
    14. file_path: String,
    15. }
    16. impl Config {
    17. // --snip--
    18. fn new(args: &[String]) -> Config {
    19. if args.len() < 3 {
    20. }
    21. // --snip--
    22. let query = args[1].clone();
    23. let file_path = args[2].clone();
    24. Config { query, file_path }
    25. }
    26. }

    Listing 12-8: Adding a check for the number of arguments

    This code is similar to the Guess::new function we wrote in Listing 9-13, where we called panic! when the value argument was out of the range of valid values. Instead of checking for a range of values here, we’re checking that the length of args is at least 3 and the rest of the function can operate under the assumption that this condition has been met. If args has fewer than three items, this condition will be true, and we call the panic! macro to end the program immediately.

    With these extra few lines of code in new, let’s run the program without any arguments again to see what the error looks like now:

    This output is better: we now have a reasonable error message. However, we also have extraneous information we don’t want to give to our users. Perhaps using the technique we used in Listing 9-13 isn’t the best to use here: a call to panic! is more appropriate for a programming problem than a usage problem, . Instead, we’ll use the other technique you learned about in Chapter 9—returning a Result that indicates either success or an error.

    We can instead return a Result value that will contain a Config instance in the successful case and will describe the problem in the error case. We’re also going to change the function name from new to build because many programmers expect new functions to never fail. When Config::build is communicating to main, we can use the Result type to signal there was a problem. Then we can change main to convert an Err variant into a more practical error for our users without the surrounding text about thread 'main' and RUST_BACKTRACE that a call to panic! causes.

    Listing 12-9 shows the changes we need to make to the return value of the function we’re now calling Config::build and the body of the function needed to return a Result. Note that this won’t compile until we update main as well, which we’ll do in the next listing.

    Filename: src/main.rs

    1. use std::env;
    2. use std::fs;
    3. fn main() {
    4. let args: Vec<String> = env::args().collect();
    5. let config = Config::new(&args);
    6. println!("Searching for {}", config.query);
    7. println!("In file {}", config.file_path);
    8. let contents = fs::read_to_string(config.file_path)
    9. .expect("Should have been able to read the file");
    10. println!("With text:\n{contents}");
    11. }
    12. struct Config {
    13. query: String,
    14. file_path: String,
    15. }
    16. impl Config {
    17. fn build(args: &[String]) -> Result<Config, &'static str> {
    18. if args.len() < 3 {
    19. return Err("not enough arguments");
    20. }
    21. let query = args[1].clone();
    22. let file_path = args[2].clone();
    23. Ok(Config { query, file_path })
    24. }
    25. }

    Listing 12-9: Returning a Result from Config::build

    Our build function returns a Result with a Config instance in the success case and a &'static str in the error case. Our error values will always be string literals that have the 'static lifetime.

    We’ve made two changes in the body of the function: instead of calling panic! when the user doesn’t pass enough arguments, we now return an Err value, and we’ve wrapped the Config return value in an Ok. These changes make the function conform to its new type signature.

    Returning an Err value from Config::build allows the main function to handle the Result value returned from the build function and exit the process more cleanly in the error case.

    Calling Config::build and Handling Errors

    To handle the error case and print a user-friendly message, we need to update main to handle the Result being returned by Config::build, as shown in Listing 12-10. We’ll also take the responsibility of exiting the command line tool with a nonzero error code away from panic! and instead implement it by hand. A nonzero exit status is a convention to signal to the process that called our program that the program exited with an error state.

    Filename: src/main.rs

    1. use std::env;
    2. use std::fs;
    3. use std::process;
    4. fn main() {
    5. let args: Vec<String> = env::args().collect();
    6. let config = Config::build(&args).unwrap_or_else(|err| {
    7. println!("Problem parsing arguments: {err}");
    8. process::exit(1);
    9. });
    10. // --snip--
    11. println!("Searching for {}", config.query);
    12. println!("In file {}", config.file_path);
    13. let contents = fs::read_to_string(config.file_path)
    14. .expect("Should have been able to read the file");
    15. println!("With text:\n{contents}");
    16. }
    17. struct Config {
    18. query: String,
    19. file_path: String,
    20. }
    21. impl Config {
    22. fn build(args: &[String]) -> Result<Config, &'static str> {
    23. if args.len() < 3 {
    24. return Err("not enough arguments");
    25. }
    26. let query = args[1].clone();
    27. let file_path = args[2].clone();
    28. Ok(Config { query, file_path })
    29. }
    30. }

    Listing 12-10: Exiting with an error code if building a Config fails

    In this listing, we’ve used a method we haven’t covered in detail yet: unwrap_or_else, which is defined on Result by the standard library. Using unwrap_or_else allows us to define some custom, non-panic! error handling. If the Result is an Ok value, this method’s behavior is similar to unwrap: it returns the inner value Ok is wrapping. However, if the value is an Err value, this method calls the code in the closure, which is an anonymous function we define and pass as an argument to unwrap_or_else. We’ll cover closures in more detail in . For now, you just need to know that unwrap_or_else will pass the inner value of the Err, which in this case is the static string "not enough arguments" that we added in Listing 12-9, to our closure in the argument err that appears between the vertical pipes. The code in the closure can then use the err value when it runs.

    We’ve added a new use line to bring process from the standard library into scope. The code in the closure that will be run in the error case is only two lines: we print the err value and then call process::exit. The process::exit function will stop the program immediately and return the number that was passed as the exit status code. This is similar to the panic!-based handling we used in Listing 12-8, but we no longer get all the extra output. Let’s try it:

    1. $ cargo run
    2. Compiling minigrep v0.1.0 (file:///projects/minigrep)
    3. Finished dev [unoptimized + debuginfo] target(s) in 0.48s
    4. Running `target/debug/minigrep`
    5. Problem parsing arguments: not enough arguments

    Great! This output is much friendlier for our users.

    Now that we’ve finished refactoring the configuration parsing, let’s turn to the program’s logic. As we stated in , we’ll extract a function named run that will hold all the logic currently in the main function that isn’t involved with setting up configuration or handling errors. When we’re done, main will be concise and easy to verify by inspection, and we’ll be able to write tests for all the other logic.

    Listing 12-11 shows the extracted run function. For now, we’re just making the small, incremental improvement of extracting the function. We’re still defining the function in src/main.rs.

    Filename: src/main.rs

    1. use std::env;
    2. use std::fs;
    3. use std::process;
    4. fn main() {
    5. // --snip--
    6. let args: Vec<String> = env::args().collect();
    7. let config = Config::build(&args).unwrap_or_else(|err| {
    8. println!("Problem parsing arguments: {err}");
    9. process::exit(1);
    10. });
    11. println!("Searching for {}", config.query);
    12. println!("In file {}", config.file_path);
    13. run(config);
    14. }
    15. fn run(config: Config) {
    16. let contents = fs::read_to_string(config.file_path)
    17. .expect("Should have been able to read the file");
    18. println!("With text:\n{contents}");
    19. }
    20. // --snip--
    21. struct Config {
    22. query: String,
    23. file_path: String,
    24. }
    25. impl Config {
    26. fn build(args: &[String]) -> Result<Config, &'static str> {
    27. if args.len() < 3 {
    28. return Err("not enough arguments");
    29. }
    30. let query = args[1].clone();
    31. let file_path = args[2].clone();
    32. Ok(Config { query, file_path })
    33. }
    34. }

    The run function now contains all the remaining logic from main, starting from reading the file. The run function takes the Config instance as an argument.

    With the remaining program logic separated into the run function, we can improve the error handling, as we did with Config::build in Listing 12-9. Instead of allowing the program to panic by calling expect, the run function will return a Result when something goes wrong. This will let us further consolidate the logic around handling errors into main in a user-friendly way. Listing 12-12 shows the changes we need to make to the signature and body of run.

    Filename: src/main.rs

    Listing 12-12: Changing the run function to return Result

    We’ve made three significant changes here. First, we changed the return type of the run function to Result<(), Box>. This function previously returned the unit type, (), and we keep that as the value returned in the Ok case.

    For the error type, we used the trait object Box (and we’ve brought std::error::Error into scope with a use statement at the top). We’ll cover trait objects in Chapter 17. For now, just know that Box means the function will return a type that implements the Error trait, but we don’t have to specify what particular type the return value will be. This gives us flexibility to return error values that may be of different types in different error cases. The dyn keyword is short for “dynamic.”

    Second, we’ve removed the call to expect in favor of the ? operator, as we talked about in . Rather than panic! on an error, ? will return the error value from the current function for the caller to handle.

    Third, the run function now returns an Ok value in the success case. We’ve declared the run function’s success type as () in the signature, which means we need to wrap the unit type value in the Ok value. This Ok(()) syntax might look a bit strange at first, but using () like this is the idiomatic way to indicate that we’re calling run for its side effects only; it doesn’t return a value we need.

    When you run this code, it will compile but will display a warning:

    1. $ cargo run the poem.txt
    2. Compiling minigrep v0.1.0 (file:///projects/minigrep)
    3. warning: unused `Result` that must be used
    4. --> src/main.rs:19:5
    5. |
    6. | ^^^^^^^^^^^^
    7. |
    8. = note: `#[warn(unused_must_use)]` on by default
    9. = note: this `Result` may be an `Err` variant, which should be handled
    10. warning: `minigrep` (bin "minigrep") generated 1 warning
    11. Finished dev [unoptimized + debuginfo] target(s) in 0.71s
    12. Running `target/debug/minigrep the poem.txt`
    13. Searching for the
    14. In file poem.txt
    15. With text:
    16. I'm nobody! Who are you?
    17. Are you nobody, too?
    18. Then there's a pair of us - don't tell!
    19. They'd banish us, you know.
    20. How dreary to be somebody!
    21. How public, like a frog
    22. To tell your name the livelong day
    23. To an admiring bog!

    Rust tells us that our code ignored the Result value and the Result value might indicate that an error occurred. But we’re not checking to see whether or not there was an error, and the compiler reminds us that we probably meant to have some error-handling code here! Let’s rectify that problem now.

    We’ll check for errors and handle them using a technique similar to one we used with Config::build in Listing 12-10, but with a slight difference:

    Filename: src/main.rs

    1. use std::env;
    2. use std::error::Error;
    3. use std::fs;
    4. use std::process;
    5. fn main() {
    6. // --snip--
    7. let args: Vec<String> = env::args().collect();
    8. let config = Config::build(&args).unwrap_or_else(|err| {
    9. println!("Problem parsing arguments: {err}");
    10. process::exit(1);
    11. });
    12. println!("Searching for {}", config.query);
    13. println!("In file {}", config.file_path);
    14. if let Err(e) = run(config) {
    15. println!("Application error: {e}");
    16. process::exit(1);
    17. }
    18. }
    19. fn run(config: Config) -> Result<(), Box<dyn Error>> {
    20. let contents = fs::read_to_string(config.file_path)?;
    21. println!("With text:\n{contents}");
    22. Ok(())
    23. }
    24. struct Config {
    25. query: String,
    26. file_path: String,
    27. }
    28. impl Config {
    29. fn build(args: &[String]) -> Result<Config, &'static str> {
    30. if args.len() < 3 {
    31. return Err("not enough arguments");
    32. }
    33. let query = args[1].clone();
    34. let file_path = args[2].clone();
    35. Ok(Config { query, file_path })
    36. }
    37. }

    We use if let rather than unwrap_or_else to check whether run returns an Err value and call process::exit(1) if it does. The run function doesn’t return a value that we want to unwrap in the same way that Config::build returns the Config instance. Because run returns () in the success case, we only care about detecting an error, so we don’t need unwrap_or_else to return the unwrapped value, which would only be ().

    The bodies of the if let and the unwrap_or_else functions are the same in both cases: we print the error and exit.

    Our minigrep project is looking good so far! Now we’ll split the src/main.rs file and put some code into the src/lib.rs file. That way we can test the code and have a src/main.rs file with fewer responsibilities.

    Let’s move all the code that isn’t the main function from src/main.rs to src/lib.rs:

    • The run function definition
    • The relevant use statements
    • The definition of Config
    • The Config::build function definition

    The contents of src/lib.rs should have the signatures shown in Listing 12-13 (we’ve omitted the bodies of the functions for brevity). Note that this won’t compile until we modify src/main.rs in Listing 12-14.

    Filename: src/lib.rs

    1. use std::error::Error;
    2. use std::fs;
    3. pub struct Config {
    4. pub query: String,
    5. pub file_path: String,
    6. }
    7. impl Config {
    8. pub fn build(args: &[String]) -> Result<Config, &'static str> {
    9. // --snip--
    10. if args.len() < 3 {
    11. return Err("not enough arguments");
    12. }
    13. let query = args[1].clone();
    14. let file_path = args[2].clone();
    15. Ok(Config { query, file_path })
    16. }
    17. }
    18. pub fn run(config: Config) -> Result<(), Box<dyn Error>> {
    19. // --snip--
    20. let contents = fs::read_to_string(config.file_path)?;
    21. println!("With text:\n{contents}");
    22. Ok(())
    23. }

    Listing 12-13: Moving Config and run into src/lib.rs

    We’ve made liberal use of the pub keyword: on Config, on its fields and its build method, and on the run function. We now have a library crate that has a public API we can test!

    Now we need to bring the code we moved to src/lib.rs into the scope of the binary crate in src/main.rs, as shown in Listing 12-14.

    Filename: src/main.rs

    1. use std::env;
    2. use std::process;
    3. use minigrep::Config;
    4. fn main() {
    5. // --snip--
    6. let args: Vec<String> = env::args().collect();
    7. let config = Config::build(&args).unwrap_or_else(|err| {
    8. println!("Problem parsing arguments: {err}");
    9. process::exit(1);
    10. });
    11. println!("Searching for {}", config.query);
    12. println!("In file {}", config.file_path);
    13. if let Err(e) = minigrep::run(config) {
    14. // --snip--
    15. println!("Application error: {e}");
    16. process::exit(1);
    17. }
    18. }

    Listing 12-14: Using the minigrep library crate in src/main.rs

    We add a use minigrep::Config line to bring the Config type from the library crate into the binary crate’s scope, and we prefix the run function with our crate name. Now all the functionality should be connected and should work. Run the program with and make sure everything works correctly.

    Whew! That was a lot of work, but we’ve set ourselves up for success in the future. Now it’s much easier to handle errors, and we’ve made the code more modular. Almost all of our work will be done in src/lib.rs from here on out.