Created
May 11, 2019 21:08
-
-
Save eknowlton/0f253d374cf99c844bdd0f69ece7f9f2 to your computer and use it in GitHub Desktop.
rust minigrep
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::env; | |
use std::error::Error; | |
use std::fs; | |
pub struct Config { | |
pub query: String, | |
pub filename: String, | |
pub case_sensitive: bool, | |
} | |
impl Config { | |
pub fn new(mut args: std::env::Args) -> Result<Config, &'static str> { | |
args.next(); | |
let query = match args.next() { | |
Some(arg) => arg, | |
None => return Err("Missing query argument"), | |
}; | |
let filename = match args.next() { | |
Some(arg) => arg, | |
None => return Err("Missing filename argument"), | |
}; | |
let case_sensitive = env::var("CASE_INSENSITIVE").is_err(); | |
Ok(Config { | |
query, | |
filename, | |
case_sensitive, | |
}) | |
} | |
} | |
pub fn run(config: Config) -> Result<(), Box<dyn Error>> { | |
let contents = fs::read_to_string(config.filename)?; | |
let results = if config.case_sensitive { | |
search(&config.query, &contents) | |
} else { | |
search_case_insensitive(&config.query, &contents) | |
}; | |
for line in results { | |
println!("{}", line); | |
} | |
Ok(()) | |
} | |
pub fn search<'a>(query: &str, contents: &'a str) -> Vec<&'a str> { | |
contents | |
.lines() | |
.filter(|line| line.contains(query)) | |
.collect() | |
} | |
pub fn search_case_insensitive<'a>(query: &str, contents: &'a str) -> Vec<&'a str> { | |
let query = query.to_lowercase(); | |
contents | |
.lines() | |
.filter(|line| line.to_lowercase().contains(query)) // expected an `FnMut<(char, )>` closure, found `std:string::String` | |
.collect() | |
} | |
#[cfg(test)] | |
mod tests { | |
use super::*; | |
#[test] | |
fn case_sensitive() { | |
let query = "duct"; | |
let contents = "\ | |
Rust: | |
safe, fast, productive. | |
Pick three. | |
Duct tape."; | |
assert_eq!(vec!["safe, fast, productive."], search(query, contents)); | |
} | |
#[test] | |
fn case_insensitive() { | |
let query = "duct"; | |
let contents = "\ | |
Rust: | |
safe, fast, productive. | |
Pick three. | |
Duct tape."; | |
assert_eq!( | |
vec!["safe, fast, productive.", "Duct tape."], | |
search_case_insensitive(query, contents) | |
); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::env; | |
use std::process; | |
use minigrep::Config; | |
fn main() { | |
let config = Config::new(env::args()).unwrap_or_else(|err| { | |
println!("Problems parsing arguments: {}", err); | |
process::exit(1); | |
}); | |
println!("Searching for {}", config.query); | |
println!("In file {}", config.filename); | |
if let Err(e) = minigrep::run(config) { | |
println!("Application error: {}", e); | |
process::exit(1); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment