mirror of https://github.com/KaiserY/trpl-zh-cn
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.
43 lines
918 B
43 lines
918 B
use std::env;
|
|
use std::fs;
|
|
// ANCHOR: here
|
|
use std::process;
|
|
|
|
fn main() {
|
|
let args: Vec<String> = env::args().collect();
|
|
|
|
let config = Config::new(&args).unwrap_or_else(|err| {
|
|
println!("Problem parsing arguments: {}", err);
|
|
process::exit(1);
|
|
});
|
|
|
|
// --snip--
|
|
// ANCHOR_END: here
|
|
|
|
println!("Searching for {}", config.query);
|
|
println!("In file {}", config.filename);
|
|
|
|
let contents = fs::read_to_string(config.filename)
|
|
.expect("Something went wrong reading the file");
|
|
|
|
println!("With text:\n{}", contents);
|
|
}
|
|
|
|
struct Config {
|
|
query: String,
|
|
filename: String,
|
|
}
|
|
|
|
impl Config {
|
|
fn new(args: &[String]) -> Result<Config, &'static str> {
|
|
if args.len() < 3 {
|
|
return Err("not enough arguments");
|
|
}
|
|
|
|
let query = args[1].clone();
|
|
let filename = args[2].clone();
|
|
|
|
Ok(Config { query, filename })
|
|
}
|
|
}
|