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, io::Write};pub struct LogQuery<'a> { logs: &'a Vec<String>,}// 1. Finish the implementation of LogQueryimpl<'a> LogQuery<'a> {// 2. Create a public associated function named `new()` that will take a reference to a vector of strings// pub fn new(logs: &'a Vec<String>) -> Self { LogQuery { logs } }// 3. Create a public method named `search` that accepts a string slice and finds it from the logs and// returns a vector of references to those 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 = File::create(path).map_err(|_| "Erreur"); let mut f = File::create(path)?; for log in logs { writeln!(f, "{log}")?; } Ok(()) }}
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 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")? } file.flush()?; Ok(()) }}
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 logs = self.search(keyword); match File::options().create(true).write(true).open(path){ Ok(mut f) => { for log in logs.iter(){ writeln!(f, "{}", log); } return Ok(()); } Err(er) => return Err(er), } }}
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 filtered_logs = self.search(keyword); let mut file = File::options().read(true).write(true).create(true).open(path)?; for log in filtered_logs { writeln!(file, "{}", log)?; } 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<()> { let mut file = fs::File::create(path)?; let logs = self.search(keyword); logs.iter().for_each(|item| { write!(file, "{item}\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 file = File::create(path)?; for log in logs { file.write_all(log.as_bytes())?; file.write_all(b"\n")?; } Ok(()) }}
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 search_result = self.search(keyword); // Open or create new file form path let mut file = OpenOptions::new() .read(true) .write(true) .create(true) .truncate(true) .open(path)?; let content: String = search_result .iter() .map(|s| s.as_str()) // Convert &String to &str .collect::<Vec<&str>>() // Collect into Vec<&str> .join("\n"); // Join with newlines file.write_all(content.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<()> { // 🎁 Your code here! 🎁 let logs = self.search(keyword); let mut file = std::fs::File::create(path)?; for line in logs { file.write(format!("{}\n", line).as_bytes())?; } Ok(()) }}
pub struct LogQuery<'a> { logs: &'a Vec<String>,}use std::path::Path;use std::fs::File;use std::io::{self, 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<()> { // 🎁 Your code here! 🎁 let matches = self.search(keyword); let fpath = Path::new(path); let mut file = File::create(&fpath)?; for log in matches { 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, 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 l in logs { writeln!(file, "{}", l)?; } 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 matches: Vec<String> = self.search(keyword).into_iter().map(|s| s.clone()).collect(); let mut file_handle = File::create(path)?; file_handle.write_all(matches.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<()> { // 🎁 Your code here! 🎁 let mut file = File::create(path)?; let relevant_logs = self.search(keyword); for log in relevant_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 output = File::create(path)?; for log in logs { writeln!(output, "{}", 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<()> { // 🎁 Your code here! 🎁 let matched_logs = self.search(keyword); let mut file = File::create(path)?; for log in matched_logs{ 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 } } 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 searched_logs = self .search(keyword) .iter() .map(|log| log.as_str()) .collect::<Vec<&str>>(); std::fs::write(path, searched_logs.join("\n")) }}
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)?; for log in self.search(keyword) { writeln!(file, "{}", log).expect("can't write to file"); } Ok(()) }}
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 keywords = self.search(keyword); let mut file = File::create(path)?; for keyword in keywords { writeln!(file, "{}", keyword)?; } 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(()) }}
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 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) { writeln!(file, "{log}")?; } Ok(()) }}
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 = match File::create(&path) { Err(why) => return Err(why), Ok(file) => file, }; //let mut result = Result::Ok(); /* for &x in self.search(&keyword).iter() { let result: std::io::Result<()> = match file.write(x.as_bytes()) { Err(why) => return Err(why), Ok(x) => Ok(()), }; }; */ let errlog = self.search(&keyword).iter().map(|s| s.as_str()) .collect::<Vec<&str>>().join("\n"); match file.write(errlog.as_bytes()) { Err(why) => return Err(why), Ok(x) => Ok(()), } //file.close() }}
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 buf = File::create(path)?; for l in logs { write!(buf, "{}\n", l)?; } buf.flush()?; Ok(()) }}
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<()> { let results = self.search(keyword); let mut output = BufWriter::new(File::create(path)?); for line in results { output.write_all(line.as_bytes())?; output.write("\n".as_bytes())?; } 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<()> { // 🎁 Your code here! 🎁 let logs_to_export = self.search(keyword); let mut file = File::create(path)?; for log in logs_to_export { 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;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 matches = self.search(keyword); let mut file = fs::File::create(path)?; for log in matches { 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) { writeln!(&mut 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 loglines = self.search(keyword); loglines.iter().try_for_each(|log| writeln!(file, "{}", log)) }}
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 content = self.search(keyword).iter().fold(String::new(), |acc, &s| acc + s.as_str() + "\n"); let mut f = fs::File::create(path)?; f.write_all(&content.as_bytes()) }}
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 wanted_logs = self .search(keyword) .iter() .map(|str| str.to_string()) .collect::<Vec<_>>() .join("\n"); std::fs::write(path, wanted_logs) }}
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 wanted_logs = self.search(keyword); for log in wanted_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 logs = self.search(keyword); let mut file = File::create(path)?; for log in logs { writeln!(file, "{}", log)?;} // 🎁 Your code here! 🎁 Ok(()) }}
use std::io::{Write, Result};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::File::create(path)?; let filtered_logs = self.search(keyword); for log in filtered_logs { writeln!(file, "{}", log)?; } // file.write_all(b"Hello, world!")?; 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<()> { let logs = self .search(keyword) .iter() .map(|line| line.to_string()) .collect::<Vec<_>>() .join("\n"); std::fs::write(path, logs) }}
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::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_new(path)?; for log in logs { 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 } } 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(()) }}
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(()) }}
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(()) }}
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(()) }}
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(()) }}
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<()>>() }}
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) }}
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(()) }}
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<()>>() }}
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(()) }}