mirror of
https://github.com/KaiserY/trpl-zh-cn
synced 2025-02-24 13:22:19 +08:00
37 lines
773 B
Rust
37 lines
773 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.file_path);
|
|
|
|
let contents = fs::read_to_string(config.file_path)
|
|
.expect("Should have been able to read the file");
|
|
|
|
println!("With text:\n{contents}");
|
|
}
|
|
|
|
struct Config {
|
|
query: String,
|
|
file_path: 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 file_path = args[2].clone();
|
|
|
|
Ok(Config { query, file_path })
|
|
}
|
|
}
|
|
// ANCHOR_END: here
|