rust
45 lines · 9 steps
Buffered log scanning in Rust
Two functions stream a log file line by line with buffered reads, aggregating stats and finding the first match without loading the whole file.
Explained by
highlit
1use std::fs::File;
2use std::io::{self, BufRead, BufReader};
3use std::path::Path;
4
5pub struct LogStats {
6 pub total_lines: usize,
7 pub error_lines: usize,
8 pub bytes_scanned: u64,
9}
10
11pub fn scan_log<P: AsRef<Path>>(path: P) -> io::Result<LogStats> {
12 let file = File::open(path)?;
13 let reader = BufReader::with_capacity(64 * 1024, file);
14
15 let mut stats = LogStats {
16 total_lines: 0,
17 error_lines: 0,
18 bytes_scanned: 0,
19 };
20
21 for line in reader.lines() {
22 let line = line?;
23 stats.total_lines += 1;
24 stats.bytes_scanned += line.len() as u64 + 1;
25
26 if line.contains("ERROR") || line.contains("FATAL") {
27 stats.error_lines += 1;
28 }
29 }
30
31 Ok(stats)
32}
33
34pub fn first_match<P: AsRef<Path>>(path: P, needle: &str) -> io::Result<Option<(usize, String)>> {
35 let reader = BufReader::new(File::open(path)?);
36
37 for (idx, line) in reader.lines().enumerate() {
38 let line = line?;
39 if line.contains(needle) {
40 return Ok(Some((idx + 1, line)));
41 }
42 }
43
44 Ok(None)
45}
01 / 01
STEP 01
‹ swipe to step through ›
Walkthrough
Space play
←→ step
click any line
Three takeaways
- 1Buffered readers let you stream large files line by line instead of loading them entirely into memory.
- 2Accepting `AsRef<Path>` makes a function callable with `&str`, `String`, or `PathBuf` interchangeably.
- 3Propagating `io::Result` with `?` keeps the happy path readable while surfacing every I/O failure.
Related explainers
rust
use std::collections::HashMap; use std::sync::{Arc, Mutex}; use std::thread;
Aggregating metrics across threads in Rust
concurrency
shared-state
mutex
Intermediate
7 steps
go
package main import ( "errors"
Parsing and validating CLI flags in Go
cli-parsing
validation
error-handling
Intermediate
8 steps
java
public class ThumbnailProcessor { private static final int MAX_CONCURRENCY = 4;
Bounded parallel thumbnail rendering in Java
concurrency
thread-pool
futures
Intermediate
7 steps
rust
use std::sync::{mpsc, Arc, Mutex}; use std::thread; use std::time::Duration;
Building a thread pool in Rust
concurrency
channels
thread-pool
Advanced
9 steps
python
import csv import io from datetime import datetime
Streaming a CSV export in Flask
streaming
generators
csv
Intermediate
9 steps
go
package cache import ( "container/list"
Building a generic LRU cache in Go
lru-cache
generics
linked-list
Intermediate
8 steps
Share this explainer
Here's the card — post it anywhere.
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code
Embed this explainer
Drop the interactive walkthrough into a blog or docs. Views never cost a credit.
<iframe src="https://highlit.co/explainers/buffered-log-scanning-in-rust-explained-rust-bc3f/embed?autoplay=1" width="100%" height="520" loading="lazy" style="border:0"></iframe>
Autoplay is on by default — add ?autoplay=0 to start paused.