summaryrefslogtreecommitdiffstats
path: root/src/doc/book/listings/ch12-an-io-project/listing-12-11
diff options
context:
space:
mode:
Diffstat (limited to 'src/doc/book/listings/ch12-an-io-project/listing-12-11')
-rw-r--r--src/doc/book/listings/ch12-an-io-project/listing-12-11/Cargo.lock6
-rw-r--r--src/doc/book/listings/ch12-an-io-project/listing-12-11/Cargo.toml6
-rw-r--r--src/doc/book/listings/ch12-an-io-project/listing-12-11/poem.txt9
-rw-r--r--src/doc/book/listings/ch12-an-io-project/listing-12-11/src/main.rs50
4 files changed, 71 insertions, 0 deletions
diff --git a/src/doc/book/listings/ch12-an-io-project/listing-12-11/Cargo.lock b/src/doc/book/listings/ch12-an-io-project/listing-12-11/Cargo.lock
new file mode 100644
index 000000000..88bf82d16
--- /dev/null
+++ b/src/doc/book/listings/ch12-an-io-project/listing-12-11/Cargo.lock
@@ -0,0 +1,6 @@
+# This file is automatically @generated by Cargo.
+# It is not intended for manual editing.
+[[package]]
+name = "minigrep"
+version = "0.1.0"
+
diff --git a/src/doc/book/listings/ch12-an-io-project/listing-12-11/Cargo.toml b/src/doc/book/listings/ch12-an-io-project/listing-12-11/Cargo.toml
new file mode 100644
index 000000000..64c2a3f52
--- /dev/null
+++ b/src/doc/book/listings/ch12-an-io-project/listing-12-11/Cargo.toml
@@ -0,0 +1,6 @@
+[package]
+name = "minigrep"
+version = "0.1.0"
+edition = "2021"
+
+[dependencies]
diff --git a/src/doc/book/listings/ch12-an-io-project/listing-12-11/poem.txt b/src/doc/book/listings/ch12-an-io-project/listing-12-11/poem.txt
new file mode 100644
index 000000000..870752731
--- /dev/null
+++ b/src/doc/book/listings/ch12-an-io-project/listing-12-11/poem.txt
@@ -0,0 +1,9 @@
+I'm nobody! Who are you?
+Are you nobody, too?
+Then there's a pair of us - don't tell!
+They'd banish us, you know.
+
+How dreary to be somebody!
+How public, like a frog
+To tell your name the livelong day
+To an admiring bog!
diff --git a/src/doc/book/listings/ch12-an-io-project/listing-12-11/src/main.rs b/src/doc/book/listings/ch12-an-io-project/listing-12-11/src/main.rs
new file mode 100644
index 000000000..3f476b975
--- /dev/null
+++ b/src/doc/book/listings/ch12-an-io-project/listing-12-11/src/main.rs
@@ -0,0 +1,50 @@
+use std::env;
+use std::fs;
+use std::process;
+
+// ANCHOR: here
+fn main() {
+ // --snip--
+
+ // ANCHOR_END: here
+ let args: Vec<String> = env::args().collect();
+
+ let config = Config::build(&args).unwrap_or_else(|err| {
+ println!("Problem parsing arguments: {err}");
+ process::exit(1);
+ });
+
+ // ANCHOR: here
+ println!("Searching for {}", config.query);
+ println!("In file {}", config.file_path);
+
+ run(config);
+}
+
+fn run(config: Config) {
+ let contents = fs::read_to_string(config.file_path)
+ .expect("Should have been able to read the file");
+
+ println!("With text:\n{contents}");
+}
+
+// --snip--
+// ANCHOR_END: here
+
+struct Config {
+ query: String,
+ file_path: String,
+}
+
+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 })
+ }
+}