summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAdam Carpenter <53hornet@gmail.com>2019-02-25 16:47:02 -0500
committerAdam Carpenter <53hornet@gmail.com>2019-02-25 16:47:02 -0500
commita0070916ef13387461a11bb26d5c798b07121165 (patch)
treea7b9d132ed90b30a9ac5c2d9773edfc487a6c31e
parent7d5500f62c1eb316682d6c8de314d2b181c5248a (diff)
downloadlearning-rust-a0070916ef13387461a11bb26d5c798b07121165.tar.xz
learning-rust-a0070916ef13387461a11bb26d5c798b07121165.zip
Updated to include Config struct.
-rw-r--r--minigrep/.gitignore3
-rw-r--r--minigrep/src/main.rs39
2 files changed, 41 insertions, 1 deletions
diff --git a/minigrep/.gitignore b/minigrep/.gitignore
new file mode 100644
index 0000000..12847b6
--- /dev/null
+++ b/minigrep/.gitignore
@@ -0,0 +1,3 @@
+target/
+Cargo.lock
+*.txt
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<String> = 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<Config, &'static str> {
+
+ if args.len() < 3 {
+ panic!("not enough args");
+ }
+
+ let query = args[1].clone();
+ let filename = args[2].clone();
+ Ok(Config { query, filename })
+ }
+
}