Working with Environment Variables

We’ll improve minigrep by adding an extra feature: an option for case-insensitive searching that the user can turn on via an environment variable. We could make this feature a command line option and require that users enter it each time they want it to apply, but instead we’ll use an environment variable. Doing so allows our users to set the environment variable once and have all their searches be case insensitive in that terminal session.

Writing a Failing Test for the Case-Insensitive search Function

We want to add a new search_case_insensitive function that we’ll call when the environment variable is on. We’ll continue to follow the TDD process, so the first step is again to write a failing test. We’ll add a new test for the new search_case_insensitive function and rename our old test from one_result to case_sensitive to clarify the differences between the two tests, as shown in Listing 12-20.

Filename: src/lib.rs

  1. # #![allow(unused_variables)]
  2. #fn main() {
  3. #[cfg(test)]
  4. mod test {
  5. use super::*;
  6. #[test]
  7. fn case_sensitive() {
  8. let query = "duct";
  9. let contents = "\
  10. Rust:
  11. safe, fast, productive.
  12. Pick three.
  13. Duct tape.";
  14. assert_eq!(
  15. vec!["safe, fast, productive."],
  16. search(query, contents)
  17. );
  18. }
  19. #[test]
  20. fn case_insensitive() {
  21. let query = "rUsT";
  22. let contents = "\
  23. Rust:
  24. safe, fast, productive.
  25. Pick three.
  26. Trust me.";
  27. assert_eq!(
  28. vec!["Rust:", "Trust me."],
  29. search_case_insensitive(query, contents)
  30. );
  31. }
  32. }
  33. #}

Listing 12-20: Adding a new failing test for the case-insensitive function we’re about to add

Note that we’ve edited the old test’s contents too. We’ve added a new line with the text "Duct tape." using a capital D that shouldn’t match the query "duct" when we’re searching in a case-sensitive manner. Changing the old test in this way helps ensure that we don’t accidentally break the case-sensitive search functionality that we’ve already implemented. This test should pass now and should continue to pass as we work on the case-insensitive search.

The new test for the case-insensitive search uses "rUsT" as its query. In the search_case_insensitive function we’re about to add, the query "rUsT" should match the line containing "Rust:" with a capital R and match the line "Trust me." even though both have different casing than the query. This is our failing test, and it will fail to compile because we haven’t yet defined the search_case_insensitive function. Feel free to add a skeleton implementation that always returns an empty vector, similar to the way we did for the search function in Listing 12-16 to see the test compile and fail.

Implementing the search_case_insensitive Function

The search_case_insensitive function, shown in Listing 12-21, will be almost the same as the search function. The only difference is that we’ll lowercase the query and each line so whatever the case of the input arguments, they’ll be the same case when we check whether the line contains the query.

Filename: src/lib.rs

  1. # #![allow(unused_variables)]
  2. #fn main() {
  3. pub fn search_case_insensitive<'a>(query: &str, contents: &'a str) -> Vec<&'a str> {
  4. let query = query.to_lowercase();
  5. let mut results = Vec::new();
  6. for line in contents.lines() {
  7. if line.to_lowercase().contains(&query) {
  8. results.push(line);
  9. }
  10. }
  11. results
  12. }
  13. #}

Listing 12-21: Defining the search_case_insensitive function to lowercase the query and the line before comparing them

First, we lowercase the query string and store it in a shadowed variable with the same name. Calling to_lowercase on the query is necessary so no matter whether the user’s query is "rust", "RUST", "Rust:", or "rUsT", we’ll treat the query as if it were "rust" and be insensitive to the case.

Note that query is now a String rather than a string slice, because calling to_lowercase creates new data rather than referencing existing data. Say the query is "rUsT", as an example: that string slice doesn’t contain a lowercase u or t for us to use, so we have to allocate a new String containing "rust". When we pass query as an argument to the contains method now, we need to add an ampersand because the signature of contains is defined to take a string slice.

Next, we add a call to to_lowercase on each line before we check whether it contains query to lowercase all characters. Now that we’ve converted line and query to lowercase, we’ll find matches no matter what the case of the query is.

Let’s see if this implementation passes the tests:

running 2 tests test test::case_insensitive ... ok test test::case_sensitive ... ok test result: ok. 2 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out

Great! They passed. Now, let’s call the new search_case_insensitive function from the run function. First, we’ll add a configuration option to the Config struct to switch between case-sensitive and case-insensitive search. Adding this field will cause compiler errors because we aren’t initializing this field anywhere yet:

Filename: src/lib.rs

  1. # #![allow(unused_variables)]
  2. #fn main() {
  3. pub struct Config {
  4. pub query: String,
  5. pub filename: String,
  6. pub case_sensitive: bool,
  7. }
  8. #}

Note that we added the case_sensitive field that holds a Boolean. Next, we need the run function to check the case_sensitive field’s value and use that to decide whether to call the search function or the search_case_insensitive function, as shown in Listing 12-22. Note this still won’t compile yet.

Filename: src/lib.rs

  1. # #![allow(unused_variables)]
  2. #fn main() {
  3. # use std::error::Error;
  4. # use std::fs::File;
  5. # use std::io::prelude::*;
  6. #
  7. # fn search<'a>(query: &str, contents: &'a str) -> Vec<&'a str> {
  8. # vec![]
  9. # }
  10. #
  11. # pub fn search_case_insensitive<'a>(query: &str, contents: &'a str) -> Vec<&'a str> {
  12. # vec![]
  13. # }
  14. #
  15. # struct Config {
  16. # query: String,
  17. # filename: String,
  18. # case_sensitive: bool,
  19. # }
  20. #
  21. pub fn run(config: Config) -> Result<(), Box<Error>> {
  22. let mut f = File::open(config.filename)?;
  23. let mut contents = String::new();
  24. f.read_to_string(&mut contents)?;
  25. let results = if config.case_sensitive {
  26. search(&config.query, &contents)
  27. } else {
  28. search_case_insensitive(&config.query, &contents)
  29. };
  30. for line in results {
  31. println!("{}", line);
  32. }
  33. Ok(())
  34. }
  35. #}

Listing 12-22: Calling either search or search_case_insensitive based on the value in config.case_sensitive

Finally, we need to check for the environment variable. The functions for working with environment variables are in the env module in the standard library, so we want to bring that module into scope with a use std::env; line at the top of src/lib.rs. Then we’ll use the var function from the env module to check for an environment variable named CASE_INSENSITIVE, as shown in Listing 12-23.

Filename: src/lib.rs

  1. # #![allow(unused_variables)]
  2. #fn main() {
  3. use std::env;
  4. # struct Config {
  5. # query: String,
  6. # filename: String,
  7. # case_sensitive: bool,
  8. # }
  9. // --snip--
  10. impl Config {
  11. pub fn new(args: &[String]) -> Result<Config, &'static str> {
  12. if args.len() < 3 {
  13. return Err("not enough arguments");
  14. }
  15. let query = args[1].clone();
  16. let filename = args[2].clone();
  17. let case_sensitive = env::var("CASE_INSENSITIVE").is_err();
  18. Ok(Config { query, filename, case_sensitive })
  19. }
  20. }
  21. #}

Listing 12-23: Checking for an environment variable named CASE_INSENSITIVE

Here, we create a new variable case_sensitive. To set its value, we call the env::var function and pass it the name of the CASE_INSENSITIVE environment variable. The env::var function returns a Result that will be the successful Ok variant that contains the value of the environment variable if the environment variable is set. It will return the Err variant if the environment variable is not set.

We’re using the is_err method on the Result to check whether it’s an error and therefore unset, which means it should do a case-sensitive search. If the CASE_INSENSITIVE environment variable is set to anything, is_err will return false and the program will perform a case-insensitive search. We don’t care about the value of the environment variable, just whether it’s set or unset, so we’re checking is_err rather than using unwrap, expect, or any of the other methods we’ve seen on Result.

We pass the value in the case_sensitive variable to the Config instance so the run function can read that value and decide whether to call search or search_case_insensitive, as we implemented in Listing 12-22.

Let’s give it a try! First, we’ll run our program without the environment variable set and with the query to, which should match any line that contains the word “to” in all lowercase:

$ cargo run to poem.txt Compiling minigrep v0.1.0 (file:///projects/minigrep) Finished dev [unoptimized + debuginfo] target(s) in 0.0 secs Running `target/debug/minigrep to poem.txt` Are you nobody, too? How dreary to be somebody!

Looks like that still works! Now, let’s run the program with CASE_INSENSITIVE set to 1 but with the same query to.

If you’re using PowerShell, you will need to set the environment variable and run the program in two commands rather than one:

$ $env:CASE_INSENSITIVE=1 $ cargo run to poem.txt

We should get lines that contain “to” that might have uppercase letters:

$ CASE_INSENSITIVE=1 cargo run to poem.txt Finished dev [unoptimized + debuginfo] target(s) in 0.0 secs Running `target/debug/minigrep to poem.txt` Are you nobody, too? How dreary to be somebody! To tell your name the livelong day To an admiring bog!

Excellent, we also got lines containing “To”! Our minigrep program can now do case-insensitive searching controlled by an environment variable. Now you know how to manage options set using either command line arguments or environment variables.

Some programs allow arguments and environment variables for the same configuration. In those cases, the programs decide that one or the other takes precedence. For another exercise on your own, try controlling case insensitivity through either a command line argument or an environment variable. Decide whether the command line argument or the environment variable should take precedence if the program is run with one set to case sensitive and one set to case insensitive.

The std::env module contains many more useful features for dealing with environment variables: check out its documentation to see what is available.