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:
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.”
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.
for log in logs {
writeln!(file, "{}", log)?;
}
Return Ok(())
if everything goes well.
use std::fs::File;use std::io::Write;pub struct LogQuery<'a> { logs: &'a Vec<String>,}impl<'a> LogQuery<'a> { pub fn new(logs: &'a Vec<String>) -> Self { LogQuery { logs } } pub fn search(&self, keyword: &str) -> Vec<&'a String> { self.logs .iter() .filter(|log| log.contains(keyword)) .collect() } pub fn export_to_file(&self, keyword: &str, path: &str) -> std::io::Result<()> { // 🎁 Your code here! 🎁 let logs_matched = self.search(keyword); // creating a file on the provided path let mut file = File::create(path)?; for log in logs_matched { file.write(format!("{}\n",log).as_bytes())?; } Ok(()) }}
pub struct LogQuery<'a> { logs: &'a Vec<String>,}impl<'a> LogQuery<'a> { pub fn new(logs: &'a Vec<String>) -> Self { LogQuery { logs } } pub fn search(&self, keyword: &str) -> Vec<&'a String> { self.logs .iter() .filter(|log| log.contains(keyword)) .collect() } pub fn export_to_file(&self, keyword: &str, path: &str) -> std::io::Result<()> { // 🎁 Your code here! 🎁 let logs = self.search(keyword); std::fs::write( path, logs.iter() .map(|s| s.as_str()) .collect::<Vec<_>>() .join("\n"), ) }}
pub struct LogQuery<'a> { logs: &'a Vec<String>,}impl<'a> LogQuery<'a> { pub fn new(logs: &'a Vec<String>) -> Self { LogQuery { logs } } pub fn search(&self, keyword: &str) -> Vec<&'a String> { self.logs .iter() .filter(|log| log.contains(keyword)) .collect() } pub fn export_to_file(&self, keyword: &str, path: &str) -> std::io::Result<()> { // 🎁 Your code here! 🎁 use std::fs::File; use std::io::Write; let logs = self.search(keyword); let mut file = File::create(path)?; for log in logs { writeln!(&mut file, "{}", log)?; } Ok(()) }}
use std::io::Write;pub struct LogQuery<'a> { logs: &'a Vec<String>,}impl<'a> LogQuery<'a> { pub fn new(logs: &'a Vec<String>) -> Self { LogQuery { logs } } pub fn search(&self, keyword: &str) -> Vec<&'a String> { self.logs .iter() .filter(|log| log.contains(keyword)) .collect() } pub fn export_to_file(&self, keyword: &str, path: &str) -> std::io::Result<()> { let logs = self.search(keyword); let mut file = std::fs::File::create(path)?; for log in logs { writeln!(file, "{}", log)?; } Ok(()) }}
pub struct LogQuery<'a> { logs: &'a Vec<String>,}use std::fs::File;use std::io::Write;impl<'a> LogQuery<'a> { pub fn new(logs: &'a Vec<String>) -> Self { LogQuery { logs } } pub fn search(&self, keyword: &str) -> Vec<&'a String> { self.logs .iter() .filter(|log| log.contains(keyword)) .collect() } pub fn export_to_file(&self, keyword: &str, path: &str) -> std::io::Result<()> { let logs = self.search(keyword); let mut file = File::create(path)?; for log in logs { writeln!(file, "{}", log); } Ok(()) // 🎁 Your code here! 🎁 }}
use std::{ fs::File, io::{BufWriter, Write},};pub struct LogQuery<'a> { logs: &'a Vec<String>,}impl<'a> LogQuery<'a> { pub fn new(logs: &'a Vec<String>) -> Self { LogQuery { logs } } pub fn search(&self, keyword: &str) -> Vec<&'a String> { self.logs .iter() .filter(|log| log.contains(keyword)) .collect() } pub fn export_to_file(&self, keyword: &str, path: &str) -> std::io::Result<()> { // 🎁 Your code here! 🎁 let logs = self.search(keyword); let file = File::create(path)?; let mut writer = BufWriter::new(file); for log in logs { writeln!(writer, "{log}")?; } Ok(()) }}
use std::{fs::File, io::Write};pub struct LogQuery<'a> { logs: &'a Vec<String>,}impl<'a> LogQuery<'a> { pub fn new(logs: &'a Vec<String>) -> Self { LogQuery { logs } } pub fn search(&self, keyword: &str) -> Vec<&'a String> { self.logs .iter() .filter(|log| log.contains(keyword)) .collect() } pub fn export_to_file(&self, keyword: &str, path: &str) -> std::io::Result<()> { // 🎁 Your code here! let logs = self.search(keyword); let mut file = File::create(path)?; for log in logs { writeln!(file, "{}", log)?; } Ok(()) }}
use std::{fs::File, io::Write};pub struct LogQuery<'a> { logs: &'a Vec<String>,}impl<'a> LogQuery<'a> { pub fn new(logs: &'a Vec<String>) -> Self { LogQuery { logs } } pub fn search(&self, keyword: &str) -> Vec<&'a String> { self.logs .iter() .filter(|log| log.contains(keyword)) .collect() } pub fn export_to_file(&self, keyword: &str, path: &str) -> std::io::Result<()> { let logs = self.search(keyword); let mut file = File::create(path)?; for log in logs { writeln!(file, "{log}")?; } Ok(()) }}
use std::fs::File;use std::io::Write;pub struct LogQuery<'a> { logs: &'a Vec<String>,}impl<'a> LogQuery<'a> { pub fn new(logs: &'a Vec<String>) -> Self { LogQuery { logs } } pub fn search(&self, keyword: &str) -> Vec<&'a String> { self.logs .iter() .filter(|log| log.contains(keyword)) .collect() } pub fn export_to_file(&self, keyword: &str, path: &str) -> std::io::Result<()> { // 🎁 Your code here! 🎁 let logs = self.search(keyword); let mut file = File::create(path)?; for log in logs{ writeln!(file,"{}",log)?; } Ok(()) }}
use std::{fs::File, io::Write};pub struct LogQuery<'a> { logs: &'a Vec<String>,}impl<'a> LogQuery<'a> { pub fn new(logs: &'a Vec<String>) -> Self { LogQuery { logs } } pub fn search(&self, keyword: &str) -> Vec<&'a String> { self.logs .iter() .filter(|log| log.contains(keyword)) .collect() } pub fn export_to_file(&self, keyword: &str, path: &str) -> std::io::Result<()> { let mut f = File::create(path)?; for log in self.search(keyword) { writeln!(f, "{log}")?; } Ok(()) }}
use std::{fs::File, io::Write};pub struct LogQuery<'a> { logs: &'a Vec<String>,}impl<'a> LogQuery<'a> { pub fn new(logs: &'a Vec<String>) -> Self { LogQuery { logs } } pub fn search(&self, keyword: &str) -> Vec<&'a String> { self.logs .iter() .filter(|log| log.contains(keyword)) .collect() } pub fn export_to_file(&self, keyword: &str, path: &str) -> std::io::Result<()> { let mut file = File::create(path)?; for log in self.search(keyword) { write!(file, "{log}\n")?; } Ok(()) }}
use std::{fs::File, io::Write};pub struct LogQuery<'a> { logs: &'a Vec<String>,}impl<'a> LogQuery<'a> { pub fn new(logs: &'a Vec<String>) -> Self { LogQuery { logs } } pub fn search(&self, keyword: &str) -> Vec<&'a String> { self.logs .iter() .filter(|log| log.contains(keyword)) .collect() } pub fn export_to_file(&self, keyword: &str, path: &str) -> std::io::Result<()> { let logs = self.search(keyword); let mut file = File::create(path)?; for log in logs { writeln!(file, "{}", log)?; } Ok(()) }}
use std::{fs::File, io::Write};pub struct LogQuery<'a> { logs: &'a Vec<String>,}impl<'a> LogQuery<'a> { pub fn new(logs: &'a Vec<String>) -> Self { LogQuery { logs } } pub fn search(&self, keyword: &str) -> Vec<&'a String> { self.logs .iter() .filter(|log| log.contains(keyword)) .collect() } pub fn export_to_file(&self, keyword: &str, path: &str) -> std::io::Result<()> { let logs = self.search(keyword); let mut file = File::create(path)?; for log in logs { writeln!(file, "{}", log)?; } Ok(()) }}
use std::{fs::File, io::Write};pub struct LogQuery<'a> { logs: &'a Vec<String>,}impl<'a> LogQuery<'a> { pub fn new(logs: &'a Vec<String>) -> Self { LogQuery { logs } } pub fn search(&self, keyword: &str) -> Vec<&'a String> { self.logs .iter() .filter(|log| log.contains(keyword)) .collect() } pub fn export_to_file(&self, keyword: &str, path: &str) -> std::io::Result<()> { let mut file = File::create(path)?; let logs = self.search(keyword); for log in logs{ writeln!(file, "{}", log)?; } Ok(()) }}
use std::fs::File;use std::io::Write;pub struct LogQuery<'a> { logs: &'a Vec<String>,}impl<'a> LogQuery<'a> { pub fn new(logs: &'a Vec<String>) -> Self { LogQuery { logs } } pub fn search(&self, keyword: &str) -> Vec<&'a String> { self.logs .iter() .filter(|log| log.contains(keyword)) .collect() } pub fn export_to_file(&self, keyword: &str, path: &str) -> std::io::Result<()> { // 🎁 Your code here! 🎁 let mut file = File::create(path)?; let logs = self.search(keyword); for log in logs { writeln!(file, "{}", log)?; } Ok(()) }}
use std::fs::File;use std::io::Write;pub struct LogQuery<'a> { logs: &'a Vec<String>,}impl<'a> LogQuery<'a> { pub fn new(logs: &'a Vec<String>) -> Self { LogQuery { logs } } pub fn search(&self, keyword: &str) -> Vec<&'a String> { self.logs .iter() .filter(|log| log.contains(keyword)) .collect() } pub fn export_to_file(&self, keyword: &str, path: &str) -> std::io::Result<()> { let mut file = File::create(path)?; let logs = self.search(keyword); for log in logs { write!(file, "{log}\n")?; } Ok(()) }}
use std::io::Write;pub struct LogQuery<'a> { logs: &'a Vec<String>,}impl<'a> LogQuery<'a> { pub fn new(logs: &'a Vec<String>) -> Self { LogQuery { logs } } pub fn search(&self, keyword: &str) -> Vec<&'a String> { self.logs .iter() .filter(|log| log.contains(keyword)) .collect() } pub fn export_to_file(&self, keyword: &str, path: &str) -> std::io::Result<()> { let search_results = self.search(keyword); let mut file = std::fs::File::create(path)?; for log in search_results.iter() { writeln!(file, "{}", log); } Ok(()) }}
use std::fs::File;use std::io::Write;pub struct LogQuery<'a> { logs: &'a Vec<String>,}impl<'a> LogQuery<'a> { pub fn new(logs: &'a Vec<String>) -> Self { LogQuery { logs } } pub fn search(&self, keyword: &str) -> Vec<&'a String> { self.logs .iter() .filter(|log| log.contains(keyword)) .collect() } pub fn export_to_file(&self, keyword: &str, path: &str) -> std::io::Result<()> { // 🎁 Your code here! 🎁 let logs = self.search(keyword); let mut file = File::create(path)?; for log in logs { writeln!(file, "{}", log)?; } Ok(()) }}
use std::fs::File;use std::io::Write;pub struct LogQuery<'a> { logs: &'a Vec<String>,}impl<'a> LogQuery<'a> { pub fn new(logs: &'a Vec<String>) -> Self { LogQuery { logs } } pub fn search(&self, keyword: &str) -> Vec<&'a String> { self.logs .iter() .filter(|log| log.contains(keyword)) .collect() } pub fn export_to_file(&self, keyword: &str, path: &str) -> std::io::Result<()> { // 🎁 Your code here! 🎁 let mut file = File::create(path)?; for log in self.search(keyword).iter() { file.write_all(log.as_bytes())?; file.write_all("\n".as_bytes())?; } Ok(()) }}
use std::io::Write;pub struct LogQuery<'a> { logs: &'a Vec<String>,}impl<'a> LogQuery<'a> { pub fn new(logs: &'a Vec<String>) -> Self { LogQuery { logs } } pub fn search(&self, keyword: &str) -> Vec<&'a String> { self.logs .iter() .filter(|log| log.contains(keyword)) .collect() } pub fn export_to_file(&self, keyword: &str, path: &str) -> std::io::Result<()> { let logs: Vec<&'a String> = self.search(keyword); let mut file = match std::fs::OpenOptions::new() .create(true) .append(true) .open(path) { Ok(f) => f, Err(e) => return Err(e), }; for log in logs { writeln!(file, "{}", log)?; } Ok(()) }}
use std::{fs::File, io::Write};pub struct LogQuery<'a> { logs: &'a Vec<String>,}impl<'a> LogQuery<'a> { pub fn new(logs: &'a Vec<String>) -> Self { LogQuery { logs } } pub fn search(&self, keyword: &str) -> Vec<&'a String> { self.logs .iter() .filter(|log| log.contains(keyword)) .collect() } pub fn export_to_file(&self, keyword: &str, path: &str) -> std::io::Result<()> { // 🎁 Your code here! 🎁 let logs = self.search(keyword); let mut file = File::create(path)?; for log in logs { writeln!(file, "{}", log)?; } Ok(()) }}
use std::fs::File;use std::io::Write;pub struct LogQuery<'a> { logs: &'a Vec<String>,}impl<'a> LogQuery<'a> { pub fn new(logs: &'a Vec<String>) -> Self { LogQuery { logs } } pub fn search(&self, keyword: &str) -> Vec<&'a String> { self.logs .iter() .filter(|log| log.contains(keyword)) .collect() } pub fn export_to_file(&self, keyword: &str, path: &str) -> std::io::Result<()> { let mut file = File::create(path)?; self.search(keyword).iter().for_each(|log| { writeln!(file, "{}", log).unwrap(); }); Ok(()) }}
use std::{fs, io::Write};pub struct LogQuery<'a> { logs: &'a Vec<String>,}impl<'a> LogQuery<'a> { pub fn new(logs: &'a Vec<String>) -> Self { LogQuery { logs } } pub fn search(&self, keyword: &str) -> Vec<&'a String> { self.logs .iter() .filter(|log| log.contains(keyword)) .collect() } pub fn export_to_file(&self, keyword: &str, path: &str) -> std::io::Result<()> { // 🎁 Your code here! 🎁 let mut f = fs::File::create(path)?; for log in self.search(keyword) { writeln!(f, "{log}")?; } Ok(()) }}
use std::fs::File;use std::io::Write;pub struct LogQuery<'a> { logs: &'a Vec<String>,}impl<'a> LogQuery<'a> { pub fn new(logs: &'a Vec<String>) -> Self { LogQuery { logs } } pub fn search(&self, keyword: &str) -> Vec<&'a String> { self.logs .iter() .filter(|log| log.contains(keyword)) .collect() } pub fn export_to_file(&self, keyword: &str, path: &str) -> std::io::Result<()> { let logs = self.search(keyword); let mut file = File::create(path)?; for line in logs { writeln!(file, "{line}")?; } Ok(()) }}
use std::fs::File;use std::io::Write;pub struct LogQuery<'a> { logs: &'a Vec<String>,}impl<'a> LogQuery<'a> { pub fn new(logs: &'a Vec<String>) -> Self { LogQuery { logs } } pub fn search(&self, keyword: &str) -> Vec<&'a String> { self.logs .iter() .filter(|log| log.contains(keyword)) .collect() } pub fn export_to_file(&self, keyword: &str, path: &str) -> std::io::Result<()> { // 🎁 Your code here! 🎁 let mut file = File::create(path)?; for line in self.search(keyword) { writeln!(file, "{line}")?; } Ok(()) }}
use std::io::Write;pub struct LogQuery<'a> { logs: &'a Vec<String>,}impl<'a> LogQuery<'a> { pub fn new(logs: &'a Vec<String>) -> Self { LogQuery { logs } } pub fn search(&self, keyword: &str) -> Vec<&'a String> { self.logs .iter() .filter(|log| log.contains(keyword)) .collect() } pub fn export_to_file(&self, keyword: &str, path: &str) -> std::io::Result<()> { // 🎁 Your code here! 🎁 let logs = self.search(keyword); let mut file = std::fs::File::create(path)?; for log in logs { writeln!(file, "{}", log)?; } Ok(()) }}
use std::{fs::File,io::Write};pub struct LogQuery<'a> { logs: &'a Vec<String>,}impl<'a> LogQuery<'a> { pub fn new(logs: &'a Vec<String>) -> Self { LogQuery { logs } } pub fn search(&self, keyword: &str) -> Vec<&'a String> { self.logs .iter() .filter(|log| log.contains(keyword)) .collect() } pub fn export_to_file(&self, keyword: &str, path: &str) -> std::io::Result<()> { let mut f = File::create(path)?; let logs = self.search(keyword); for log in logs { writeln!(f, "{}", log)?; } Ok(()) }}
pub struct LogQuery<'a> { logs: &'a Vec<String>,}impl<'a> LogQuery<'a> { pub fn new(logs: &'a Vec<String>) -> Self { LogQuery { logs } } pub fn search(&self, keyword: &str) -> Vec<&'a String> { self.logs .iter() .filter(|log| log.contains(keyword)) .collect() } pub fn export_to_file(&self, keyword: &str, path: &str) -> std::io::Result<()> { // 🎁 Your code here! 🎁 use std::io::Write; let mut file = std::fs::File::create(path)?; for log in self.search(keyword) { writeln!(file, "{log}")?; } Ok(()) }}
use std::io::Write;pub struct LogQuery<'a> { logs: &'a Vec<String>,}impl<'a> LogQuery<'a> { pub fn new(logs: &'a Vec<String>) -> Self { LogQuery { logs } } pub fn search(&self, keyword: &str) -> Vec<&'a String> { self.logs .iter() .filter(|log| log.contains(keyword)) .collect() } pub fn export_to_file(&self, keyword: &str, path: &str) -> std::io::Result<()> { let mut file = std::fs::File::create(path)?; for log in self.search(keyword) { writeln!(file, "{}", log)?; } Ok(()) }}
use std::{fs::File, io::Write};pub struct LogQuery<'a> { logs: &'a Vec<String>,}impl<'a> LogQuery<'a> { pub fn new(logs: &'a Vec<String>) -> Self { LogQuery { logs } } pub fn search(&self, keyword: &str) -> Vec<&'a String> { self.logs .iter() .filter(|log| log.contains(keyword)) .collect() } pub fn export_to_file(&self, keyword: &str, path: &str) -> std::io::Result<()> { let logs = self.search(keyword); let mut file = File::create(path)?; for log in logs { writeln!(file, "{}", log)?; } Ok(()) }}
use std::{fs::File, io::Write};pub struct LogQuery<'a> { logs: &'a Vec<String>,}impl<'a> LogQuery<'a> { pub fn new(logs: &'a Vec<String>) -> Self { LogQuery { logs } } pub fn search(&self, keyword: &str) -> Vec<&'a String> { self.logs .iter() .filter(|log| log.contains(keyword)) .collect() } pub fn export_to_file(&self, keyword: &str, path: &str) -> std::io::Result<()> { // 🎁 Your code here! 🎁 let target_logs = self.search(keyword); let mut file = File::create(path)?; for log in target_logs { writeln!(file, "{}", log)?; } Ok(()) }}
use std::{fs::File, io::Write};pub struct LogQuery<'a> { logs: &'a Vec<String>,}impl<'a> LogQuery<'a> { pub fn new(logs: &'a Vec<String>) -> Self { LogQuery { logs } } pub fn search(&self, keyword: &str) -> Vec<&'a String> { self.logs .iter() .filter(|log| log.contains(keyword)) .collect() } pub fn export_to_file(&self, keyword: &str, path: &str) -> std::io::Result<()> { // 🎁 Your code here! 🎁 let mut file = File::create(path)?; let logs: Vec<&'a String> = self.search(keyword); for log in logs { writeln!(file, "{}", log)?; } Ok(()) }}
use std::fs::File;use std::io::Write;pub struct LogQuery<'a> { logs: &'a Vec<String>,}impl<'a> LogQuery<'a> { pub fn new(logs: &'a Vec<String>) -> Self { LogQuery { logs } } pub fn search(&self, keyword: &str) -> Vec<&'a String> { self.logs .iter() .filter(|log| log.contains(keyword)) .collect() } pub fn export_to_file(&self, keyword: &str, path: &str) -> std::io::Result<()> { // 🎁 Your code here! 🎁 let exports = self.search(keyword); let mut file = File::create(path)?; for item in exports { writeln!(file, "{}", item).expect("Unable to write to file"); } Ok(()) }}
use std::{fs, io::Write};pub struct LogQuery<'a> { logs: &'a Vec<String>,}impl<'a> LogQuery<'a> { pub fn new(logs: &'a Vec<String>) -> Self { LogQuery { logs } } pub fn search(&self, keyword: &str) -> Vec<&'a String> { self.logs .iter() .filter(|log| log.contains(keyword)) .collect() } pub fn export_to_file(&self, keyword: &str, path: &str) -> std::io::Result<()> { // 🎁 Your code here! 🎁 let mut f = fs::File::create(path)?; for log in self.search(keyword) { writeln!(f, "{}", log)?; } return Ok(()); }}
use std::io::Write;pub struct LogQuery<'a> { logs: &'a Vec<String>,}impl<'a> LogQuery<'a> { pub fn new(logs: &'a Vec<String>) -> Self { LogQuery { logs } } pub fn search(&self, keyword: &str) -> Vec<&'a String> { self.logs .iter() .filter(|log| log.contains(keyword)) .collect() } pub fn export_to_file(&self, keyword: &str, path: &str) -> std::io::Result<()> { // 🎁 Your code here! 🎁 let logs = self.search(keyword); let mut f = std::fs::OpenOptions::new().create(true).write(true).open(path)?; for log in logs { writeln!(f, "{}", log)?; } Ok(()) }}
use std::fs::File;use std::io::Write;pub struct LogQuery<'a> { logs: &'a Vec<String>,}impl<'a> LogQuery<'a> { pub fn new(logs: &'a Vec<String>) -> Self { LogQuery { logs } } pub fn search(&self, keyword: &str) -> Vec<&'a String> { self.logs .iter() .filter(|log| log.contains(keyword)) .collect() } pub fn export_to_file(&self, keyword: &str, path: &str) -> std::io::Result<()> { let mut file = File::create(path)?; let matches = self.search(keyword); // Won't work as closure must return () not Result // matches.iter().for_each(|line| writeln!(file, "{line}")); for line in matches { writeln!(file, "{line}")?; } Ok(()) }}
use std::io::Write;pub struct LogQuery<'a> { logs: &'a Vec<String>,}impl<'a> LogQuery<'a> { pub fn new(logs: &'a Vec<String>) -> Self { LogQuery { logs } } pub fn search(&self, keyword: &str) -> Vec<&'a String> { self.logs .iter() .filter(|log| log.contains(keyword)) .collect() } pub fn export_to_file(&self, keyword: &str, path: &str) -> std::io::Result<()> { // 🎁 Your code here! 🎁 let findings = self.search(keyword); let mut file = std::fs::File::create(path)?; for data in findings { writeln!(file, "{}", data)?; } Ok(()) }}
use std::{fs::File, io::Write};pub struct LogQuery<'a> { logs: &'a Vec<String>,}impl<'a> LogQuery<'a> { pub fn new(logs: &'a Vec<String>) -> Self { LogQuery { logs } } pub fn search(&self, keyword: &str) -> Vec<&'a String> { self.logs .iter() .filter(|log| log.contains(keyword)) .collect() } pub fn export_to_file(&self, keyword: &str, path: &str) -> std::io::Result<()> { let logs = self.search(keyword); let mut file = File::create(path)?; for log in logs { writeln!(file, "{}", log)?; } Ok(()) }}
use std::{fs::File, io::Write};pub struct LogQuery<'a> { logs: &'a Vec<String>,}impl<'a> LogQuery<'a> { pub fn new(logs: &'a Vec<String>) -> Self { LogQuery { logs } } pub fn search(&self, keyword: &str) -> Vec<&'a String> { self.logs .iter() .filter(|log| log.contains(keyword)) .collect() } pub fn export_to_file(&self, keyword: &str, path: &str) -> std::io::Result<()> { // 🎁 Your code here! 🎁 let mut file = File::create(path)?; for log in self.search(keyword) { writeln!(file, "{}", log)?; } Ok(()) }}
use std::{fs::File, io::Result, io::Write};pub struct LogQuery<'a> { logs: &'a Vec<String>,}impl<'a> LogQuery<'a> { pub fn new(logs: &'a Vec<String>) -> Self { LogQuery { logs } } pub fn search(&self, keyword: &str) -> Vec<&'a String> { self.logs .iter() .filter(|log| log.contains(keyword)) .collect() } pub fn export_to_file(&self, keyword: &str, path: &str) -> Result<()> { let logs = self.search(keyword); let mut file = File::create(path)?; for log in logs { file.write_all(format!("{}\n", log).as_bytes())?; } Ok(()) }}
use std::fs::File;use std::io::BufWriter;use std::io::prelude::*;pub struct LogQuery<'a> { logs: &'a Vec<String>,}impl<'a> LogQuery<'a> { pub fn new(logs: &'a Vec<String>) -> Self { LogQuery { logs } } pub fn search(&self, keyword: &str) -> Vec<&'a String> { self.logs .iter() .filter(|log| log.contains(keyword)) .collect() } pub fn export_to_file(&self, keyword: &str, path: &str) -> std::io::Result<()> { // 🎁 Your code here! 🎁 let mut file = BufWriter::new(File::create(path)?); let nl = [b'\n']; for line in self.search(keyword) { let _ = file.write(line.as_bytes())?; let _ = file.write(&nl); } file.flush()?; Ok(()) }}
use std::fs::File;use std::io::BufWriter;use std::io::prelude::*;pub struct LogQuery<'a> { logs: &'a Vec<String>,}impl<'a> LogQuery<'a> { pub fn new(logs: &'a Vec<String>) -> Self { LogQuery { logs } } pub fn search(&self, keyword: &str) -> Vec<&'a String> { self.logs .iter() .filter(|log| log.contains(keyword)) .collect() } pub fn export_to_file(&self, keyword: &str, path: &str) -> std::io::Result<()> { // 🎁 Your code here! 🎁 let mut file = BufWriter::new(File::create(path)?); let nl = [b'\n']; for line in self.search(keyword) { let _ = file.write(line.as_bytes())?; let _ = file.write(&nl); } file.flush()?; Ok(()) }}
use std::fs::File;use std::io::Write;pub struct LogQuery<'a> { logs: &'a Vec<String>,}impl<'a> LogQuery<'a> { pub fn new(logs: &'a Vec<String>) -> Self { LogQuery { logs } } pub fn search(&self, keyword: &str) -> Vec<&'a String> { self.logs .iter() .filter(|log| log.contains(keyword)) .collect() } pub fn export_to_file(&self, keyword: &str, path: &str) -> std::io::Result<()> { let logs = self.search(keyword); let mut file = File::create_new(path)?; for log in logs { writeln!(file, "{}", log)?; } Ok(()) }}
use std::{fs::File, io::{BufWriter, Write}};pub struct LogQuery<'a> { logs: &'a Vec<String>,}impl<'a> LogQuery<'a> { pub fn new(logs: &'a Vec<String>) -> Self { LogQuery { logs } } pub fn search(&self, keyword: &str) -> Vec<&'a String> { self.logs .iter() .filter(|log| log.contains(keyword)) .collect() } pub fn export_to_file(&self, keyword: &str, path: &str) -> std::io::Result<()> { let file = File::create(path)?; let mut writer = BufWriter::new(file); for line in self.search(keyword) { let bytes_with_newline = [line.as_bytes(), b"\n"].concat(); writer.write_all(&bytes_with_newline).expect("Error writing to file"); } writer.flush()?; Ok(()) }}
use std::{fs::File, io::{BufWriter, Write}};pub struct LogQuery<'a> { logs: &'a Vec<String>,}impl<'a> LogQuery<'a> { pub fn new(logs: &'a Vec<String>) -> Self { LogQuery { logs } } pub fn search(&self, keyword: &str) -> Vec<&'a String> { self.logs .iter() .filter(|log| log.contains(keyword)) .collect() } pub fn export_to_file(&self, keyword: &str, path: &str) -> std::io::Result<()> { let file = File::create(path)?; let mut writer = BufWriter::new(file); for line in self.search(keyword) { let bytes_with_newline = [line.as_bytes(), b"\n"].concat(); writer.write_all(&bytes_with_newline).expect("Error writing to file"); } writer.flush()?; Ok(()) }}
use std::{fs::File, io::{BufWriter, Write}};pub struct LogQuery<'a> { logs: &'a Vec<String>,}impl<'a> LogQuery<'a> { pub fn new(logs: &'a Vec<String>) -> Self { LogQuery { logs } } pub fn search(&self, keyword: &str) -> Vec<&'a String> { self.logs .iter() .filter(|log| log.contains(keyword)) .collect() } pub fn export_to_file(&self, keyword: &str, path: &str) -> std::io::Result<()> { let file = File::create(path)?; let mut writer = BufWriter::new(file); for line in self.search(keyword) { writer.write_all(line.as_bytes()).expect("Error writing to file"); writer.write_all(b"\n").expect("Error writing newline"); } writer.flush()?; Ok(()) }}
use std::io::Write;pub struct LogQuery<'a> { logs: &'a Vec<String>,}impl<'a> LogQuery<'a> { pub fn new(logs: &'a Vec<String>) -> Self { LogQuery { logs } } pub fn search(&self, keyword: &str) -> Vec<&'a String> { self.logs .iter() .filter(|log| log.contains(keyword)) .collect() } pub fn export_to_file(&self, keyword: &str, path: &str) -> std::io::Result<()> { let logs = self.search(keyword); let mut file = std::fs::File::create(path)?; for log in logs { writeln!(file, "{}", log)?; } Ok(()) }}
use std::fs;pub struct LogQuery<'a> { logs: &'a Vec<String>,}impl<'a> LogQuery<'a> { pub fn new(logs: &'a Vec<String>) -> Self { LogQuery { logs } } pub fn search(&self, keyword: &str) -> Vec<&'a String> { self.logs .iter() .filter(|log| log.contains(keyword)) .collect() } pub fn export_to_file(&self, keyword: &str, path: &str) -> std::io::Result<()> { // 🎁 Your code here! 🎁 let found = self.search(keyword); let content = found .iter() .map(|x| x.as_str()) .collect::<Vec<&str>>() .join("\n"); fs::write(path, content)?; Ok(()) }}
use std::{fs::File, io::Write};pub struct LogQuery<'a> { logs: &'a Vec<String>,}impl<'a> LogQuery<'a> { pub fn new(logs: &'a Vec<String>) -> Self { LogQuery { logs } } pub fn search(&self, keyword: &str) -> Vec<&'a String> { self.logs .iter() .filter(|log| log.contains(keyword)) .collect() } pub fn export_to_file(&self, keyword: &str, path: &str) -> std::io::Result<()> { // 🎁 Your code here! 🎁 let results = self.search(keyword); let mut file = File::create(path)?; for result in results { file.write_all(result.as_bytes())?; file.write_all(b"\n")?; } Ok(()) }}
use std::fs::File;use std::io::Write;pub struct LogQuery<'a> { logs: &'a Vec<String>,}impl<'a> LogQuery<'a> { pub fn new(logs: &'a Vec<String>) -> Self { LogQuery { logs } } pub fn search(&self, keyword: &str) -> Vec<&'a String> { self.logs .iter() .filter(|log| log.contains(keyword)) .collect() } pub fn export_to_file(&self, keyword: &str, path: &str) -> std::io::Result<()> { // 🎁 Your code here! 🎁 let logs = &self.search(keyword); let mut buffer = File::create(path)?; for log in logs { writeln!(buffer, "{}", log)?; } Ok(()) }}