Day 7 The Great Log Hunt
The tension in the North Pole Dev room had not dissipated. Santa’s absence loomed large, and Blitzen had clearly let the power go to his antlers. The elves were beginning to mutter about mutiny—especially after Blitzen had loudly declared that grep was overrated and "real devs" write their own search tools.
“We need a better log system,” Blitzen announced, pacing in front of the DevOps board like a caffeinated startup founder. “I’m tired of manually combing through logs! It’s time we automate this.”
Prancer peeked up from their desk. “Can’t we just pipe the logs into grep like everyone else?”
Blitzen’s glare could have melted the polar ice caps. “Prancer, if you’re going to suggest mediocre solutions, you can go back to working in Node.js.”
Prancer recoiled, whispering, “Too far, Blitzen. Too far.”
A LogQuery Tool
Blitzen wanted a log search tool so advanced that even Santa would call it “blitzening fast.” Logs were piling up from every North Pole subsystem—Toy Tracker 3000, SleighOS, and even Reindeer AI. The elves needed to find specific entries without scrolling for hours.
“You!” Blitzen pointed at Frostbyte, the elf known for typing faster than a Model M keyboard. “You’re going to write a LogQuery struct in Rust that can search through our logs.”
Frostbyte cracked his knuckles, opened NeoVim, and got to work.
But he needs your help to be saved from Blitzen’s sass and implement the LogQuery struct with its search method?
The Requirements:
Here’s what Frostbyte must implement:
- LogQuery struct: This struct should hold a reference to a slice of
Stringlogs. - new() Create an associated function named
newthat accepts a reference to aVec<String>and returns aLogQuery. - Search method: A method named
searchthat:- Returns a
Vecof references to strings containing thekeyword. - Should work even if the logs are empty.
- Special characters in the
keywordmust be handled properly.
- Returns a
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.
-
Your
LogQuerystruct will likely hold a&'a Vec<String>.pub struct LogQuery<'a> { logs: &'a Vec<String>, } -
The
newfunction should accept a reference to a&'a Vec<String>and return aLogQuery. -
The
searchmethod should accept a&selfand akeyword: &strparameter. -
To return references to the logs, you can use
-> Vec<&'a String> -
Implement
searchby iterating overself.logs. e.g.self.logs.iter() -
Use
filterand provide a closure. e.g.filter(|log| {}) -
Use
containsto check if a log contains thekeyword. e.g.log.contains(keyword) -
Use
collectto convert the iterator back to aVec. e.g.collect::<Vec<_>>()
Run to execute your code and see test results here.