The North Poleās DevOps room was unusually quietāwell, as quiet as it could be with Blitzen strutting around like he owned the place. The reindeer had declared himself Tech Leadā¢ (again), and with Santa still off on his "vision quest", nobody had dared challenge him. Every five minutes, Blitzen would casually remind the room, "Iām the Tech Lead, in case anyone forgot."
The elves, however, had not forgotten. Frostbyte, the teamās fastest typist, was already regretting showing up for work today.
It all started when Blitzen stumbled upon a string of unusual log entries:
He squinted at the terminal, his antlers practically buzzing with excitement. āThisā¦ this is big,ā Blitzen declared dramatically. āThese errors need to be isolated, analyzed, and stored in their own file. This could save Christmas!ā
Prancer, barely looking up from their desk, muttered, āCouldnāt we just pipe the logs through grep ERROR
and append it to a file with >> file.log
?ā
Blitzen whipped around, scandalized. āPrancer, what part of 'Iām the Tech Lead' didnāt you understand? We donāt use grep
here. We build solutions. Innovative solutions. In Rust!ā
āHereās the plan,ā Blitzen said, pacing like a founder pitching to VCs. āWeāll extend our LogQuery
tool to not just search logs, but also export specific entries to a file. A Rustacean-grade solution, not some bash script hack job.ā
An elf raised their hand timidly. āBut why?ā
Blitzen grinned. āBecause Iām the Tech Lead.ā
You must come to the elves rescue! Implement the export_to_file(&self, keyword: &str, file_path: &str)
method in LogQuery
that:
search
method we created earlier to get logs matching the keyword
.path
.If youāre stuck or need a starting point, here are some hints to help you along the way!
Make sure you import the necessary modules. e.g., use std::{fs::File, io::Write};
.
Get the logs using the search
method we created in the previous challenge. e.g. let logs = self.search(keyword);
.
Create a mutable file using File::create
. e.g. let mut file = File::create(path)?;
Properly handle errors with Result
and ?
to propagate them.
Loop over the logs and use the writeln!
macro to write to the file. e.g.
Return Ok(())
if everything goes well.