use std::env; use std::fs; use std::process; // ANCHOR: here fn main() { // --snip-- // ANCHOR_END: here let args: Vec = env::args().collect(); let config = Config::new(&args).unwrap_or_else(|err| { println!("Problem parsing arguments: {}", err); process::exit(1); }); // ANCHOR: here println!("Searching for {}", config.query); println!("In file {}", config.filename); run(config); } fn run(config: Config) { let contents = fs::read_to_string(config.filename) .expect("Something went wrong reading the file"); println!("With text:\n{}", contents); } // --snip-- // ANCHOR_END: here struct Config { query: String, filename: String, } impl Config { fn new(args: &[String]) -> Result { if args.len() < 3 { return Err("not enough arguments"); } let query = args[1].clone(); let filename = args[2].clone(); Ok(Config { query, filename }) } }