Compare commits

..
Author SHA1 Message Date
Ian Rettig 5e04f9c5c3 Feat: Use BufReader for better io efficiency. 2025-06-06 14:23:49 -04:00
+8 -9
View File
@@ -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,21 +9,18 @@ struct Cli {
path: std::path::PathBuf,
}
#[derive(Debug)]
struct CustomError(String);
fn main() -> Result<(), CustomError> {
fn main() -> std::io::Result<()> {
let args = Cli::parse();
// let content = std::fs::read_to_string(&args.path).expect("Could not read file.");
let f = File::open(&args.path)?;
let mut reader = BufReader::new(f);
let mut line = String::new();
let content = std::fs::read_to_string(&args.path)
.map_err(|err| CustomError(format!("Error reading `{}`: {}", args.path.display(), err)))?;
for line in content.lines() {
while reader.read_line(&mut line)? > 0 {
if line.contains(&args.pattern) {
println!("{}", line)
}
line.clear();
}
Ok(())