[Project] - Following the CLI program from rustbook

This commit is contained in:
adnanioricce 2024-12-28 09:23:41 -03:00
parent c77249b6c8
commit 1a7550137b
3 changed files with 39 additions and 0 deletions

6
minigrep/Cargo.toml Normal file

@ -0,0 +1,6 @@
[package]
name = "minigrep"
version = "0.1.0"
edition = "2021"
[dependencies]

23
minigrep/src/main.rs Normal file

@ -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<String> = 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}");
}

10
minigrep/src/poem.txt Normal file

@ -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!