Day 8: Blitzen's Debug Blitz
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.
The Discovery of Errors
It all started when Blitzen stumbled upon a string of unusual log entries:
ERROR: Toy Tracker 3000 overheating.
ERROR: SleighOS failed to authenticate.
ERROR: Reindeer AI unable to locate Prancer.
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.ā
Your Mission
You must come to the elves rescue! Implement the export_to_file(&self, keyword: &str, file_path: &str) method in LogQuery that:
- Use the
searchmethod we created earlier to get logs matching thekeyword. - Loop over the returned logs and write them to a file, the location is given by the parameter
path. - Handles Errors: If the file canāt be created or written to, the function should return an appropriate error instead of crashing.
Hints
If youāre stuck or need a starting point, here are some hints to help you along the way!
Click here to show hints.
-
Make sure you import the necessary modules. e.g.,
use std::{fs::File, io::Write};. -
Get the logs using the
searchmethod 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
Resultand?to propagate them. -
Loop over the logs and use the
writeln!macro to write to the file. e.g.for log in logs { writeln!(file, "{}", log)?; } -
Return
Ok(())if everything goes well.
Run to execute your code and see test results here.