Compare commits
2
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
5e04f9c5c3 | ||
|
|
6e23ccc0cc |
+15
-2
@@ -1,4 +1,6 @@
|
||||
use clap::Parser;
|
||||
use std::fs::File;
|
||||
use std::io::{BufRead, BufReader};
|
||||
|
||||
// Search for a pattern in a file and display the lines that contain it.
|
||||
#[derive(Parser)]
|
||||
@@ -7,8 +9,19 @@ struct Cli {
|
||||
path: std::path::PathBuf,
|
||||
}
|
||||
|
||||
fn main() {
|
||||
fn main() -> std::io::Result<()> {
|
||||
let args = Cli::parse();
|
||||
|
||||
println!("pattern: {:?}, path: {:?}", args.pattern, args.path)
|
||||
let f = File::open(&args.path)?;
|
||||
let mut reader = BufReader::new(f);
|
||||
let mut line = String::new();
|
||||
|
||||
while reader.read_line(&mut line)? > 0 {
|
||||
if line.contains(&args.pattern) {
|
||||
println!("{}", line)
|
||||
}
|
||||
line.clear();
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user