Compare commits
5
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f9418783d4 | ||
|
|
d7d2c74b23 | ||
|
|
d41577ef7d | ||
|
|
3ff1454fb6 | ||
|
|
a663f2b4d8 |
+9
-8
@@ -1,6 +1,4 @@
|
|||||||
use clap::Parser;
|
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.
|
// Search for a pattern in a file and display the lines that contain it.
|
||||||
#[derive(Parser)]
|
#[derive(Parser)]
|
||||||
@@ -9,18 +7,21 @@ struct Cli {
|
|||||||
path: std::path::PathBuf,
|
path: std::path::PathBuf,
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() -> std::io::Result<()> {
|
#[derive(Debug)]
|
||||||
|
struct CustomError(String);
|
||||||
|
|
||||||
|
fn main() -> Result<(), CustomError> {
|
||||||
let args = Cli::parse();
|
let args = Cli::parse();
|
||||||
|
|
||||||
let f = File::open(&args.path)?;
|
// let content = std::fs::read_to_string(&args.path).expect("Could not read file.");
|
||||||
let mut reader = BufReader::new(f);
|
|
||||||
let mut line = String::new();
|
|
||||||
|
|
||||||
while reader.read_line(&mut line)? > 0 {
|
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() {
|
||||||
if line.contains(&args.pattern) {
|
if line.contains(&args.pattern) {
|
||||||
println!("{}", line)
|
println!("{}", line)
|
||||||
}
|
}
|
||||||
line.clear();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|||||||
Reference in New Issue
Block a user