mirror of https://github.com/rust-lang/book
37 lines
769 B
Rust
37 lines
769 B
Rust
use std::env;
|
|
use std::fs;
|
|
|
|
fn main() {
|
|
let args: Vec<String> = env::args().collect();
|
|
|
|
let config = Config::new(&args);
|
|
|
|
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,
|
|
}
|
|
|
|
// ANCHOR: here
|
|
impl Config {
|
|
fn build(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 })
|
|
}
|
|
}
|
|
// ANCHOR_END: here
|