Compare commits
1
Commits
main
...
optimize-reading
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
5e04f9c5c3 |
+10
-3
@@ -1,4 +1,6 @@
|
|||||||
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)]
|
||||||
@@ -7,14 +9,19 @@ struct Cli {
|
|||||||
path: std::path::PathBuf,
|
path: std::path::PathBuf,
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() -> std::io::Result<()> {
|
||||||
let args = Cli::parse();
|
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();
|
||||||
|
|
||||||
for line in content.lines() {
|
while reader.read_line(&mut line)? > 0 {
|
||||||
if line.contains(&args.pattern) {
|
if line.contains(&args.pattern) {
|
||||||
println!("{}", line)
|
println!("{}", line)
|
||||||
}
|
}
|
||||||
|
line.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user