Up to 12.4

This commit is contained in:
Ada Werefox 2023-03-11 22:34:45 +00:00
parent 3daeedf6f9
commit c56cdc37fc
5 changed files with 65 additions and 2 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
/target

7
Cargo.lock generated Normal file
View File

@ -0,0 +1,7 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "minigrep"
version = "0.1.0"

9
poem.txt Normal file
View File

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

28
src/lib.rs Normal file
View File

@ -0,0 +1,28 @@
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)?;
println!("With text:\n{}", contents);
Ok(())
}

View File

@ -1,3 +1,21 @@
use std::env;
use std::process;
use minigrep::Config;
fn main() {
println!("Hello, world!");
}
let args: Vec<String> = env::args().collect();
let config = Config::build(&args).unwrap_or_else(|err| {
println!("Problem parsing arguments: {err}");
process::exit(1);
});
println!("Searching for {}", config.query);
println!("In file {}", config.file_path);
if let Err(e) = minigrep::run(config) {
println!("Application error: {e}");
process::exit(1);
}
}