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.
jgpaiva
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 v = self.search(keyword); use std::io::prelude::*; let mut file = std::fs::File::create(path)?; for v in v { file.write_all(v.as_bytes())?; file.write_all(b"\n"); } Ok(()) }}
MCsamurai034
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 = std::fs::OpenOptions::new() .append(true) .create(true) .open(path)?; let matched_logs: Vec<&String> = LogQuery::search(self, keyword); for l in matched_logs { file.write_all(l.as_bytes())?; file.write_all(b"\n")?; } Ok(()) }}
MCsamurai034
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 = std::fs::OpenOptions::new() .append(true) .create(true) .open(path)?; let matched_logs: Vec<&String> = LogQuery::search(self, keyword); for l in matched_logs { file.write_all(l.as_bytes())?; file.write_all(b"\n")?; } Ok(()) }}
MCsamurai034
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 = std::fs::OpenOptions::new() .append(true) .create(true) .open(path)?; let matched_logs: Vec<&String> = LogQuery::search(self, keyword); for l in matched_logs { file.write_all(l.as_bytes())?; file.write_all(b"\n")?; } Ok(()) }}
isaaclv
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 filtered_logs = self.search(keyword); let mut file = std::fs::File::create(path)?; for log in filtered_logs { file.write_all(log.as_bytes())?; file.write_all(b"\n")?; } Ok(()) }}
mei28
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 matches = self.search(keyword); matches .into_iter() .map(|k| writeln!(file, "{}", k)) .collect::<std::io::Result<()>>() }}
kapaseker
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).iter().map(|arg: &&String| String::as_str(*arg)).collect::<Vec<_>>().join("\n"); std::fs::write(path, logs) }}
hagl
use std::fs::File;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<()> { let mut file = File::create(path)?; for s in self.search(keyword) { file.write(s.as_bytes())?; file.write("\n".as_bytes())?; }; Ok(()) }}
mladen5000
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::Write; let mut f = File::create(path)?; let matches: Vec<_> = self.search(keyword); matches .into_iter() .map(|m| writeln!(f, "{}", m)) .collect::<std::io::Result<()>>() }}
Ferdinanddb
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 matched_logs = self.search(keyword); let mut file = File::create(path)?; for log in matched_logs { writeln!(file, "{}", log)?; } Ok(()) }}
doroshtapgh
use std::fs::File;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 = File::create(path)?; for log in self.search(keyword).iter() { writeln!(file, "{}", log)?; } Ok(()) }}
json-stateham
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(()) }}
chokkoyamada
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 { file.write_all(log.as_bytes())?; file.write_all(b"\n")?; } Ok(()) }}
chokkoyamada
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 { file.write_all(log.as_bytes())?; file.write_all(b"\n")?; } Ok(()) }}
BetkeJ
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 search_result = self.search(keyword); let mut f = File::create(path)?; for log in search_result.iter() { writeln!(f, "{}", log)?; } Ok(()) }}
Ankit8848
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 similar = self.search(keyword); let mut file = File::create(path)?; for log in similar { writeln!(file, "{}", log)?; } Ok(()) }}
habu1010
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(()) }}
rjensen
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(()) }}
rjensen
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(()) }}
uRTLy
use std::fs::File;use std::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 hits = self.search(keyword); let file = match File::create(path) { Ok(file) => { println!("File {} created", path); file }, Err(why) => { return Err(why); }, }; let mut writer = BufWriter::new(file); for hit in hits { match writeln!(writer, "{}", hit) { Ok(_) => { println!("Wrote {} to {}", hit, path); }, Err(why) => { return Err(why); }, } } Ok(()) }}
arm01846
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 matched = self.search(keyword); let mut file = File::create(path)?; for log in matched { writeln!(file, "{}", log)?; } Ok(()) }}
ankeetparikh
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 search_results = self.search(&keyword); let mut file = File::create(path)?; for log in search_results { writeln!(file, "{}", log)?; } Ok(()) }}
eguefif
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 errors = self.search(keyword); let mut file = File::create(path)?; for log in errors.iter() { writeln!(file, "{}", log)?; } Ok(()) }}
Nismirno
use std::fs::File;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 found = self.search(keyword); let mut file = match File::create(path) { Ok(f) => f, Err(e) => return Err(e), }; let content = found.iter().map(|s| s.as_str()).collect::<Vec<_>>().join("\n"); let bytes = content.as_bytes(); file.write_all(bytes) }}
tvdu29
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); } return Ok(()) }}
franlopezm
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(()) }}
arilet
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 output = File::create(path)?; let matches = self.search(keyword); for m in matches { writeln!(output, "{}", m)?; } Ok(()) }}
MaoX-Yu
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(()) }}
KushnerykPavel
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(()) }}
felipesaruhashi
use std::{fs::File, io::Write, io};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 log_content = self.search(keyword); if log_content.is_empty() { io::Error::new(io::ErrorKind::NotFound, "no matched logs"); } let mut file = match File::create_new(path) { Ok(f) => f, Err(e) => return Err(e), }; for log in log_content { match writeln!(file, "{}", log) { Ok(_) => (), Err(e) => return Err(e), } } Ok(()) }}
mrtnhwtt
use std::{fs::File, io::Write, io};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 matched_logs = self.search(keyword); if matched_logs.is_empty() { io::Error::new(io::ErrorKind::NotFound, "no matched logs"); } let mut file = match File::create_new(path) { Ok(f) => f, Err(e) => return Err(e), }; for log in matched_logs { match writeln!(file, "{}", log) { Ok(_) => (), Err(e) => return Err(e), } } Ok(()) }}
tamanishi
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 = match File::create(path) { Ok(f) => f, Err(e) => return Err(e), }; for log in logs { match writeln!(file, "{}", log) { Ok(_) => (), Err(e) => return Err(e), } }; Ok(()) }}
tunamagur0
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 matches = self.search(keyword); let mut file = File::create(path)?; for line in matches { writeln!(file, "{}", line)?; } file.flush()?; Ok(()) }}
gmvar
use std::{fs::File, io::Write, path::Path};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 path = Path::new(path); let mut file = File::create(&path)?; let logs = self.search(keyword).into_iter(); for log in logs { writeln!(file, "{}", log)?; } Ok(()) }}
hafihaf123
use std::fs::OpenOptions;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 = OpenOptions::new() .create(true) .write(true) .open(path)?; for log in self.search(keyword) { writeln!(file, "{log}")?; } Ok(()) }}
rirze
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 log in logs { writeln!(file, "{}", log)?; } Ok(()) }}
JohnMayes
use std::fs;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 log_vec = self.search(&keyword); let mut file = fs::File::create(path)?; for log in log_vec { writeln!(file, "{}", log)?; } Ok(()) }}
KLcpb
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(()) }}
iamsahu
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(()) }}
kanakshilledar
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::{self, Write}; let matching_logs = self.search(keyword); let mut file = File::create(path)?; for log in matching_logs { writeln!(file, "{}", log)?; } Ok(()) }}
phate45
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 s = self.search(keyword); let mut file = std::fs::File::create(path)?; for i in s { file.write_all(i.as_ref())?; file.write(b"\n")?; } file.sync_all() }}
sashaaKr
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<()> { let logs = self.search(&keyword); let mut file = std::fs::File::create(&path)?; let logs_as_str: Vec<&str> = logs.iter().map(|s| s.as_str()).collect(); file.write_all(logs_as_str.join("\n").as_bytes())?; Ok(()) }}
sarub0b0
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(()) }}
wlabranche
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(()) }}
RedNapPanda
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) { writeln!(file, "{}", log)?; } Ok(()) }}
strachan
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 keyword_logs = self.search(keyword); let mut f = std::fs::File::create_new(path)?; for entry in keyword_logs { writeln!(f, "{}", entry)?; } Ok(()) }}
galenseilis
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)?; } return Ok(()) }}
lulingar
use std::fs;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 matches = self.search(keyword); let mut file = fs::File::options() .create(true) .append(true) .open(path)?; for entry in matches { writeln!(&mut file, "{}", entry)?; } Ok(()) }}
jsdm13
use std::fs::File;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<()> { let mut log_file = File::create(path)?; for entry in self.search(keyword).iter() { writeln!(log_file, "{}", entry)?; } Ok(()) }}
ekreutz
use std::fs::File;use std::io::{Write, BufWriter};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(path)?; let w = &mut BufWriter::new(f); self.search(keyword) .iter() .for_each(|s| { writeln!(w, "{}", s); }); // Use `flush` to ensure all buffered operations have // been applied to the underlying writer. w.flush()?; Ok(()) }}