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 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(()) }}
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 f = File::create_new(path)?; let logs = &self.search(keyword); for log in logs { f.write_all(log.as_bytes())?; f.write_all("\n".as_bytes())?; } Ok(()) }}
pub struct LogQuery<'a> { logs: &'a Vec<String>,}use std::{fs::File, 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 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 results = self.search(keyword); for line in results { file.write_all(line.as_bytes())?; file.write_all(b"\n")?; } Ok(()) }}
use std::io::Write;use std::fs::File;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 result in self.search(keyword) { writeln!(file, "{}", result)?; } 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 fhandler = File::create(path)?; let filtered_logs = self.search(keyword); for log in filtered_logs { writeln!(fhandler,"{}",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 results = self.search(keyword); let mut file = File::create(path)?; for result in results { writeln!(file, "{}", result)?; } 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::options() .create(true) .append(true) .open(path)?; self.search(keyword) .iter() .try_for_each(|s| writeln!(file, "{}", s)) }}
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! 🎁 std::fs::File::create(path)? .write_all(&self.search(keyword) .iter() .map(|&item| item.as_bytes()) .collect::<Vec<&[u8]>>() .join("\n".as_bytes()) ) }}
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! 🎁 File::create(path)? .write_all( &self.search(keyword) .iter() .map(|x| x.as_bytes()) .collect::<Vec<&[u8]>>() .join("\n".as_bytes())) }}
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 contents = self.search(keyword); let mut file = File::create(path)?; for log in contents { writeln!(file, "{}", log)?; } Ok(()) }}
pub struct LogQuery<'a> { logs: &'a Vec<String>,}impl<'a> LogQuery<'a> { pub fn new(logs: &'a Vec<String>) -> Self { LogQuery { logs } } #[inline(always)] fn search2(&self, keyword: &'a str) -> impl Iterator<Item=&'a String> + '_ { self.logs .iter() .filter(move |log| log.contains(keyword)) } pub fn search(&self, keyword: &'a str) -> Vec<&'a String> { self .search2(keyword) .collect() } pub fn export_to_file(&self, keyword: &str, path: &str) -> std::io::Result<()> { use std::io::{BufWriter, Write}; use std::fs::File; let mut file = BufWriter::new(File::create(path)?); for s in self.search2(keyword) { file.write_all(s.as_bytes())?; file.write_all("\n".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 } } #[inline(always)] fn search2(&self, keyword: &'a str) -> impl Iterator<Item=&'a String> + '_ { self.logs .iter() .filter(move |log| log.contains(keyword)) } pub fn search(&self, keyword: &'a str) -> Vec<&'a String> { self .search2(keyword) .collect() } pub fn export_to_file(&self, keyword: &str, path: &str) -> std::io::Result<()> { use std::io::Write; let mut file = std::fs::File::create(path)?; for s in self.search2(keyword) { file.write_all(s.as_bytes())?; file.write_all("\n".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 } } fn search2(&self, keyword: &'a str) -> impl Iterator<Item=&'a String> + '_ { self.logs .iter() .filter(move |log| log.contains(keyword)) } pub fn search(&self, keyword: &'a str) -> Vec<&'a String> { self .search2(keyword) .collect() } pub fn export_to_file(&self, keyword: &str, path: &str) -> std::io::Result<()> { use std::io::Write; let mut file = std::fs::File::create(path)?; for s in self.search2(keyword) { file.write_all(s.as_bytes())?; file.write_all("\n".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 } } fn search2(&self, keyword: &'a str) -> impl Iterator<Item=&'a String> + '_ { self.logs .iter() .filter(move |log| log.contains(keyword)) } pub fn search(&self, keyword: &'a str) -> Vec<&'a String> { self .search2(keyword) .collect() } pub fn export_to_file(&self, keyword: &str, path: &str) -> std::io::Result<()> { use std::io::Write; let mut file = std::fs::File::create(path)?; for s in self.search2(keyword) { file.write_all(s.as_bytes())?; file.write_all("\n".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<()> { use std::io::Write; let mut file = std::fs::File::create(path)?; for s in self.search(keyword) { writeln!(file, "{}", s)?; } 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 results = self.search(keyword); let mut f = File::create(path)?; for result in results { f.write_all(result.as_bytes())?; f.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 result = self.search(keyword); let mut file = std::fs::File::create(path)?; for log in result { 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)?; for log in self.search(keyword) { writeln!(file, "{}", log)?; } Ok(()) }}
use std::io::Write;use std::fs::File;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 filtered_logs = Self::search(&self, keyword); let mut file = File::create(path)?; for log in filtered_logs { match file.write(log.as_bytes()) { std::io::Result::Ok(_) => (), std::io::Result::Err(ee) => return Err(ee), } match file.write("\n".as_bytes()) { std::io::Result::Ok(_) => (), std::io::Result::Err(ee) => return Err(ee), } } 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 mut buffer = File::create(path)?; for log in self.search(keyword) { buffer.write_all(log.as_bytes())?; buffer.write_all(b"\n")?; } 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<()> { use std::fs::File; use std::io::{Result, Write}; let logs = self.search(keyword); let mut file = File::create(path)?; for log in logs { writeln!(file, "{}", log)?; } Result::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 lines = self.search(keyword); let mut file = File::create(path)?; for line in lines { writeln!(file, "{}", line)?; } return Ok(()) } // pub fn export_to_file(&self, keyword: &str, path: &str) -> std::io::Result<()> { // let logs = self.search(keyword); // let mut f = File::create(path)?; // for &log in logs.iter() { // f.write_all(format!("{}\n", log).as_bytes())?; // } // return 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 output_file = File::create(path); match output_file { Ok(mut file) => Ok(self.write_file(&mut file, logs)), Err(err) => Err(err) } } fn write_file(&self, file: &mut File, logs: Vec<&'a String>) { for log_line in logs { writeln!(file, "{}", log_line); } }}
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 f = File::create(path)?; for &log in logs.iter() { f.write_all(format!("{}\n", log).as_bytes())?; } return 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 result = self.search(keyword); let mut file = File::create(path)?; for line in result { writeln!(file, "{}", line)?; } 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}").expect("Error"); } 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 = self.search(keyword); for log in logs { writeln!(file, "{log}").expect("Error"); } 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 i in self.search(keyword).iter() { writeln!(file, "{}", i)?; } 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::{fs::File, io::Write}; let mut file = File::create(path)?; for log in self.search(keyword) { writeln!(file, "{}", log)?; } Ok(()) }}
use std::{fs::File, io::BufWriter};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 filtered = self.search(keyword); let mut writer = BufWriter::new(File::create(path)?); for log in filtered { writeln!(writer, "{log}")?; } Ok(()) }}