From 1a7550137bb82f8fd496cf45e00fd3eaa5ae0f88 Mon Sep 17 00:00:00 2001 From: adnanioricce Date: Sat, 28 Dec 2024 09:23:41 -0300 Subject: [PATCH] [Project] - Following the CLI program from rustbook --- minigrep/Cargo.toml | 6 ++++++ minigrep/src/main.rs | 23 +++++++++++++++++++++++ minigrep/src/poem.txt | 10 ++++++++++ 3 files changed, 39 insertions(+) create mode 100644 minigrep/Cargo.toml create mode 100644 minigrep/src/main.rs create mode 100644 minigrep/src/poem.txt diff --git a/minigrep/Cargo.toml b/minigrep/Cargo.toml new file mode 100644 index 0000000..64c2a3f --- /dev/null +++ b/minigrep/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "minigrep" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/minigrep/src/main.rs b/minigrep/src/main.rs new file mode 100644 index 0000000..3709ea3 --- /dev/null +++ b/minigrep/src/main.rs @@ -0,0 +1,23 @@ +use std::env; +use std::fs; +struct Config { + query: String, + file_path: String, +} +fn parse_config(args: &[String]) -> Config { + let query = args[1].clone(); + let file_path = args[2].clone(); + Config { query, file_path } +} +fn main() { + let args: Vec = env::args().collect(); + let config = parse_config(&args); + + println!("Searching for {}", config.query); + println!("In file {}", config.file_path); + + let contents = + fs::read_to_string(config.file_path).expect("Should have been able to read the file"); + + println!("With text:\n {contents}"); +} diff --git a/minigrep/src/poem.txt b/minigrep/src/poem.txt new file mode 100644 index 0000000..7aaea3d --- /dev/null +++ b/minigrep/src/poem.txt @@ -0,0 +1,10 @@ +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! +