]> git.proxmox.com Git - rustc.git/blob - src/doc/book/listings/ch12-an-io-project/output-only-02-missing-lifetimes/src/lib.rs
New upstream version 1.50.0+dfsg1
[rustc.git] / src / doc / book / listings / ch12-an-io-project / output-only-02-missing-lifetimes / src / lib.rs
1 use std::error::Error;
2 use std::fs;
3
4 pub struct Config {
5 pub query: String,
6 pub filename: String,
7 }
8
9 impl Config {
10 pub fn new(args: &[String]) -> Result<Config, &str> {
11 if args.len() < 3 {
12 return Err("not enough arguments");
13 }
14
15 let query = args[1].clone();
16 let filename = args[2].clone();
17
18 Ok(Config { query, filename })
19 }
20 }
21
22 pub fn run(config: Config) -> Result<(), Box<dyn Error>> {
23 let contents = fs::read_to_string(config.filename)?;
24
25 Ok(())
26 }
27
28 // ANCHOR: here
29 pub fn search(query: &str, contents: &str) -> Vec<&str> {
30 vec![]
31 }
32 // ANCHOR_END: here
33
34 #[cfg(test)]
35 mod tests {
36 use super::*;
37
38 #[test]
39 fn one_result() {
40 let query = "duct";
41 let contents = "\
42 Rust:
43 safe, fast, productive.
44 Pick three.";
45
46 assert_eq!(vec!["safe, fast, productive."], search(query, contents));
47 }
48 }