Example project from the rust book.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

38 lines
1000 B

extern crate grep;
use std::env;
use std::fs::File;
use std::io::prelude::*;
struct Config {
query: String,
filename: String,
}
fn main() {
let args: Vec<String> = env::args().collect();
let conf = parse_config(&args);
println!("{:?} {:?}", conf.query, conf.filename);
let mut f = File::open(&conf.filename).expect(&("file not found: ".to_owned() + &conf.filename));
//let mut f = File::open(filename).expect(&format!("file not found: {}", &filename));
let mut contents = String::new();
f.read_to_string(&mut contents)
.expect("something went wrong reading the file");
println!("Searching for '{}'", conf.query);
println!("In file '{}'", conf.filename);
println!("With text:\n{}", contents);
}
//fn parse_config(args: &[String]) -> (&String, &String)
//fn parse_config(args: &[String]) -> (&str, &str)
fn parse_config(args: &[String]) -> Config {
Config {
query: args[1].clone(),
filename: args[2].clone(),
}
}