Up to 12.4
This commit is contained in:
parent
3daeedf6f9
commit
c56cdc37fc
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
/target
|
7
Cargo.lock
generated
Normal file
7
Cargo.lock
generated
Normal 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
9
poem.txt
Normal 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
28
src/lib.rs
Normal 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(())
|
||||||
|
}
|
22
src/main.rs
22
src/main.rs
@ -1,3 +1,21 @@
|
|||||||
|
use std::env;
|
||||||
|
use std::process;
|
||||||
|
|
||||||
|
use minigrep::Config;
|
||||||
|
|
||||||
fn main() {
|
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);
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user