~irimi1/learning-rust

18836e79c4b7f090b6711a22327054147278e3b9 — Manuel Groß 2 years ago 7ad2157
minigrep: Use config struct instead of tuple
1 files changed, 13 insertions(+), 8 deletions(-)

M projects/minigrep/src/main.rs
M projects/minigrep/src/main.rs => projects/minigrep/src/main.rs +13 -8
@@ 4,20 4,25 @@ use std::fs;
fn main() {
    let args: Vec<String> = env::args().collect();

    let (query, filename) = parse_config(&args);
    let config = parse_config(&args);

    println!("Searching for {}", query);
    println!("In file {}", filename);
    println!("Searching for {}", config.query);
    println!("In file {}", config.filename);

    let contents = fs::read_to_string(filename)
    let contents = fs::read_to_string(config.filename)
        .expect("Something went wrong reading the file");

    println!("With text:\n{}", contents);
}

fn parse_config(args: &[String]) -> (&str, &str) {
    let query = &args[1];
    let filename = &args[2];
struct Config {
    query: String,
    filename: String,
}

fn parse_config(args: &[String]) -> Config {
    let query = args[1].clone();
    let filename = args[2].clone();

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