mirror of
https://github.com/KaiserY/trpl-zh-cn
synced 2025-02-24 13:22:19 +08:00
39 lines
785 B
Rust
Executable File
39 lines
785 B
Rust
Executable File
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,
|
|
}
|
|
|
|
impl Config {
|
|
// ANCHOR: here
|
|
// --snip--
|
|
fn new(args: &[String]) -> Config {
|
|
if args.len() < 3 {
|
|
panic!("not enough arguments");
|
|
}
|
|
// --snip--
|
|
// ANCHOR_END: here
|
|
|
|
let query = args[1].clone();
|
|
let filename = args[2].clone();
|
|
|
|
Config { query, filename }
|
|
}
|
|
}
|