From a0070916ef13387461a11bb26d5c798b07121165 Mon Sep 17 00:00:00 2001 From: Adam Carpenter <53hornet@gmail.com> Date: Mon, 25 Feb 2019 16:47:02 -0500 Subject: Updated to include Config struct. --- minigrep/src/main.rs | 39 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) (limited to 'minigrep/src') diff --git a/minigrep/src/main.rs b/minigrep/src/main.rs index e7a11a9..1e04b5e 100644 --- a/minigrep/src/main.rs +++ b/minigrep/src/main.rs @@ -1,3 +1,40 @@ +use std::process; +use std::env; +use std::fs; + fn main() { - println!("Hello, world!"); + let args: Vec = env::args().collect(); + let config = Config::new(&args).unwrap_or_else(|err| { + println!("Problem parsing arguments: {}", err); + process::exit(1); + }); + + println!("Searching for {}", config.query); + println!("In file {}", config.filename); + + let contents = fs::read_to_string(config.filename) + .expect("something went wrong."); + + dbg!(contents); +} + +struct Config { + query: String, + filename: String, +} + +impl Config { + + + fn new(args: &[String]) -> Result { + + if args.len() < 3 { + panic!("not enough args"); + } + + let query = args[1].clone(); + let filename = args[2].clone(); + Ok(Config { query, filename }) + } + } -- cgit v1.2.3