]> git.proxmox.com Git - rustc.git/blob - src/doc/book/listings/ch12-an-io-project/listing-12-08/src/main.rs
New upstream version 1.63.0+dfsg1
[rustc.git] / src / doc / book / listings / ch12-an-io-project / listing-12-08 / src / main.rs
1 use std::env;
2 use std::fs;
3
4 fn main() {
5 let args: Vec<String> = env::args().collect();
6
7 let config = Config::new(&args);
8
9 println!("Searching for {}", config.query);
10 println!("In file {}", config.file_path);
11
12 let contents = fs::read_to_string(config.file_path)
13 .expect("Should have been able to read the file");
14
15 println!("With text:\n{contents}");
16 }
17
18 struct Config {
19 query: String,
20 file_path: String,
21 }
22
23 impl Config {
24 // ANCHOR: here
25 // --snip--
26 fn new(args: &[String]) -> Config {
27 if args.len() < 3 {
28 panic!("not enough arguments");
29 }
30 // --snip--
31 // ANCHOR_END: here
32
33 let query = args[1].clone();
34 let file_path = args[2].clone();
35
36 Config { query, file_path }
37 }
38 }