summaryrefslogtreecommitdiffstats
path: root/src/doc/book/listings/ch12-an-io-project/listing-12-19
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:02:58 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:02:58 +0000
commit698f8c2f01ea549d77d7dc3338a12e04c11057b9 (patch)
tree173a775858bd501c378080a10dca74132f05bc50 /src/doc/book/listings/ch12-an-io-project/listing-12-19
parentInitial commit. (diff)
downloadrustc-698f8c2f01ea549d77d7dc3338a12e04c11057b9.tar.xz
rustc-698f8c2f01ea549d77d7dc3338a12e04c11057b9.zip
Adding upstream version 1.64.0+dfsg1.upstream/1.64.0+dfsg1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'src/doc/book/listings/ch12-an-io-project/listing-12-19')
-rw-r--r--src/doc/book/listings/ch12-an-io-project/listing-12-19/Cargo.lock6
-rw-r--r--src/doc/book/listings/ch12-an-io-project/listing-12-19/Cargo.toml6
-rw-r--r--src/doc/book/listings/ch12-an-io-project/listing-12-19/output.txt22
-rw-r--r--src/doc/book/listings/ch12-an-io-project/listing-12-19/poem.txt9
-rw-r--r--src/doc/book/listings/ch12-an-io-project/listing-12-19/src/lib.rs58
-rw-r--r--src/doc/book/listings/ch12-an-io-project/listing-12-19/src/main.rs18
6 files changed, 119 insertions, 0 deletions
diff --git a/src/doc/book/listings/ch12-an-io-project/listing-12-19/Cargo.lock b/src/doc/book/listings/ch12-an-io-project/listing-12-19/Cargo.lock
new file mode 100644
index 000000000..88bf82d16
--- /dev/null
+++ b/src/doc/book/listings/ch12-an-io-project/listing-12-19/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-19/Cargo.toml b/src/doc/book/listings/ch12-an-io-project/listing-12-19/Cargo.toml
new file mode 100644
index 000000000..64c2a3f52
--- /dev/null
+++ b/src/doc/book/listings/ch12-an-io-project/listing-12-19/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-19/output.txt b/src/doc/book/listings/ch12-an-io-project/listing-12-19/output.txt
new file mode 100644
index 000000000..9b2078cb8
--- /dev/null
+++ b/src/doc/book/listings/ch12-an-io-project/listing-12-19/output.txt
@@ -0,0 +1,22 @@
+$ cargo test
+ Compiling minigrep v0.1.0 (file:///projects/minigrep)
+ Finished test [unoptimized + debuginfo] target(s) in 1.22s
+ Running unittests src/lib.rs (target/debug/deps/minigrep-9cd200e5fac0fc94)
+
+running 1 test
+test tests::one_result ... ok
+
+test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s
+
+ Running unittests src/main.rs (target/debug/deps/minigrep-9cd200e5fac0fc94)
+
+running 0 tests
+
+test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s
+
+ Doc-tests minigrep
+
+running 0 tests
+
+test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s
+
diff --git a/src/doc/book/listings/ch12-an-io-project/listing-12-19/poem.txt b/src/doc/book/listings/ch12-an-io-project/listing-12-19/poem.txt
new file mode 100644
index 000000000..870752731
--- /dev/null
+++ b/src/doc/book/listings/ch12-an-io-project/listing-12-19/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-19/src/lib.rs b/src/doc/book/listings/ch12-an-io-project/listing-12-19/src/lib.rs
new file mode 100644
index 000000000..f5d3ffa9f
--- /dev/null
+++ b/src/doc/book/listings/ch12-an-io-project/listing-12-19/src/lib.rs
@@ -0,0 +1,58 @@
+use std::error::Error;
+use std::fs;
+
+pub struct Config {
+ pub query: String,
+ pub file_path: String,
+}
+
+impl Config {
+ pub 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 })
+ }
+}
+
+pub fn run(config: Config) -> Result<(), Box<dyn Error>> {
+ let contents = fs::read_to_string(config.file_path)?;
+
+ Ok(())
+}
+
+// ANCHOR: here
+// ANCHOR: ch13
+pub fn search<'a>(query: &str, contents: &'a str) -> Vec<&'a str> {
+ let mut results = Vec::new();
+
+ for line in contents.lines() {
+ if line.contains(query) {
+ results.push(line);
+ }
+ }
+
+ results
+}
+// ANCHOR_END: ch13
+// ANCHOR_END: here
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn one_result() {
+ let query = "duct";
+ let contents = "\
+Rust:
+safe, fast, productive.
+Pick three.";
+
+ assert_eq!(vec!["safe, fast, productive."], search(query, contents));
+ }
+}
diff --git a/src/doc/book/listings/ch12-an-io-project/listing-12-19/src/main.rs b/src/doc/book/listings/ch12-an-io-project/listing-12-19/src/main.rs
new file mode 100644
index 000000000..a4f8a7411
--- /dev/null
+++ b/src/doc/book/listings/ch12-an-io-project/listing-12-19/src/main.rs
@@ -0,0 +1,18 @@
+use std::env;
+use std::process;
+
+use minigrep::Config;
+
+fn main() {
+ let args: Vec<String> = env::args().collect();
+
+ let config = Config::build(&args).unwrap_or_else(|err| {
+ println!("Problem parsing arguments: {err}");
+ process::exit(1);
+ });
+
+ if let Err(e) = minigrep::run(config) {
+ println!("Application error: {e}");
+ process::exit(1);
+ }
+}