~irimi1/learning-rust

1465559edf8fd519322a9bbf8bc523e059cfb103 — Manuel Groß 2 years ago 5e4c5c9
minigrep: Make error output more user friendly
1 files changed, 8 insertions(+), 4 deletions(-)

M projects/minigrep/src/main.rs
M projects/minigrep/src/main.rs => projects/minigrep/src/main.rs +8 -4
@@ 1,10 1,14 @@
use std::env;
use std::fs;
use std::process;

fn main() {
    let args: Vec<String> = env::args().collect();

    let config = Config::new(&args);
    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);


@@ 21,13 25,13 @@ struct Config {
}

impl Config {
    fn new(args: &[String]) -> Config {
    fn new(args: &[String]) -> Result<Config, &'static str> {
        if args.len() < 3 {
            panic!("Not enough arguments");
            return Err("not enough arguments");
        }
        let query = args[1].clone();
        let filename = args[2].clone();

        Config { query, filename }
        Ok(Config { query, filename })
    }
}