rust tutorial: nicer error message
This commit is contained in:
parent
91358013ef
commit
6d3537c084
19
src/main.rs
19
src/main.rs
@ -1,19 +1,22 @@
|
||||
#![allow(unused)]
|
||||
use clap::Parser;
|
||||
|
||||
/// Search for a pattern in a file and display the lines that contain it.
|
||||
#[derive(Parser)]
|
||||
|
||||
struct Cli {
|
||||
pattern: String,
|
||||
url: String,
|
||||
path: std::path::PathBuf,
|
||||
}
|
||||
|
||||
fn main() {
|
||||
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
let args = Cli::parse();
|
||||
println!("pattern: {}", args.pattern);
|
||||
println!(
|
||||
"path: {}",
|
||||
args.path.into_os_string().into_string().unwrap()
|
||||
);
|
||||
let result = std::fs::read_to_string(&args.path);
|
||||
let content = match result {
|
||||
Ok(content) => content,
|
||||
Err(error) => {
|
||||
return Err(error.into());
|
||||
}
|
||||
};
|
||||
println!("file content: {}", content);
|
||||
Ok(())
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user