Rust allows structs to hold mutable references to owned data, enabling them to modify the data in-place. This requires a deep understanding of lifetimes and Rust's borrowing rules to ensure memory safety.
In this challenge, you will implement a struct named MutableTextFinder that holds a mutable reference to a String. This struct will allow for both searching and modifying the content of the String.
The MutableTextFinder struct should provide the following functionality:
new: Creates a new instance of MutableTextFinder with the given content.find_first: Searches for the first line containing a given keyword and returns it as an immutable reference (Option<&str>).replace_lines: Replaces all lines containing a given keyword with a replacement string.get_text: Returns the reference to the content.// 1. Finish the struct definitionpub struct MutableTextFinder<'a> { pub text: &'a mut String,}// 2. Implement the methods for the structimpl<'a> MutableTextFinder<'a> { pub fn new(text: &'a mut String) -> Self { Self { text } } pub fn find_first(&self, keyword: &str) -> Option<&str> { self.text.lines().find(|s| s.contains(keyword)) } pub fn replace_lines(&mut self, keyword: &str, replacement: &str) { *self.text = self.text.lines().map(|s| { if s.contains(keyword) { replacement } else { s } }).collect::<Vec<_>>().join("\n") } pub fn get_text(&self) -> &str { self.text }}// Example usagepub fn main() { let mut text = String::from("Rust is awesome\nLearning Rust\nFun with Rustaceans"); let mut finder = MutableTextFinder::new(&mut text); let first = finder.find_first("Rust"); println!("{:?}", first); // Should print: Some("Rust is awesome") finder.replace_lines("Rust", "Programming in Rust"); println!("{}", finder.get_text()); // Should print the modified text}// 1. Finish the struct definitionpub struct MutableTextFinder<'a> { pub text: &'a mut String,}// 2. Implement the methods for the structimpl<'a> MutableTextFinder<'a> { pub fn new(text: &'a mut String) -> Self { Self {text} } pub fn find_first(&self, keyword: &str) -> Option<&str> { self.text.lines().find(|line| line.contains(keyword)) } pub fn replace_lines(&mut self, from: &str, to: &str) { let replaced: Vec<&str> = self.text.lines().map(|line| if line.contains(from) {to} else {line}).collect(); *self.text = replaced.join("\n"); } pub fn get_text(&self) -> &str { &self.text }}// Example usagepub fn main() { let mut text = String::from("Rust is awesome\nLearning Rust\nFun with Rustaceans"); let mut finder = MutableTextFinder::new(&mut text); let first = finder.find_first("Rust"); println!("{:?}", first); // Should print: Some("Rust is awesome") finder.replace_lines("Rust", "Programming in Rust"); println!("{}", finder.get_text()); // Should print the modified text}pub struct MutableTextFinder<'a> { pub text: &'a mut String,}impl<'a> MutableTextFinder<'a> { pub fn new(text: &'a mut String) -> Self { MutableTextFinder { text } } pub fn find_first(&self, keyword: &str) -> Option<&str> { self.text.lines().find(|line| line.contains(keyword)) } pub fn replace_lines(&mut self, keyword: &str, replacement: &str) -> () { *self.text = self .text .lines() .map(|line| { if line.contains(keyword) { replacement } else { line } }) .collect::<Vec<_>>() .join("\n"); } pub fn get_text(&self) -> &str { self.text }}// Example usagepub fn main() { let mut text = String::from("Rust is awesome\nLearning Rust\nFun with Rustaceans"); let mut finder = MutableTextFinder::new(&mut text); let first = finder.find_first("Rust"); println!("{:?}", first); // Should print: Some("Rust is awesome") finder.replace_lines("Rust", "Programming in Rust"); println!("{}", finder.get_text()); // Should print the modified text}// 1. Finish the struct definitionpub struct MutableTextFinder <'a> { pub text : &'a mut String,}impl <'a> MutableTextFinder <'a> { pub fn new(new_str : &'a mut String) -> Self { return MutableTextFinder { text : new_str }; } pub fn find_first(&self,find_str : &str) -> Option<&str> { self.text.lines().find(|x| x.contains(find_str)) } pub fn replace_lines(&mut self,find_str : &str, replace_str: &str) { *self.text=self.text.lines().map(|x| if x.contains(find_str) { replace_str } else { x }) .collect::<Vec<&str>>() .join("\n"); //map(|x| if x == find_str { return replace_str;} else { return x;}).collect::<Vec<str>>().join("\n") } pub fn get_text(&self) -> &str { self.text }}// 2. Implement the methods for the struct// Example usagepub fn main() { let mut text = String::from("Rust is awesome\nLearning Rust\nFun with Rustaceans"); let mut finder = MutableTextFinder::new(&mut text); let first = finder.find_first("Rust"); println!("{:?}", first); // Should print: Some("Rust is awesome") finder.replace_lines("Rust", "Programming in Rust"); println!("{}", finder.get_text()); // Should print the modified text}pub struct MutableTextFinder<'a> { text: &'a mut String,}impl<'a> MutableTextFinder<'a> { pub fn new(text: &'a mut String) -> Self { MutableTextFinder { text } } pub fn find_first(&self, keyword: &str) -> Option<&str> { self.text.lines().find(|line| line.contains(keyword)) } pub fn replace_lines(&mut self, keyword: &str, new_line: &str) { let text = self .text .lines() .map(|line| { if line.contains(keyword) { new_line } else { line } }) .collect::<Vec<_>>(); *self.text = text.join("\n"); } pub fn get_text(&self) -> &str { self.text }}pub fn main() { let mut text = String::from("Rust is awesome\nLearning Rust\nFun with Rustaceans"); let mut finder = MutableTextFinder::new(&mut text); let first = finder.find_first("Rust"); println!("{:?}", first); // Should print: Some("Rust is awesome") finder.replace_lines("Rust", "Programming in Rust"); println!("{}", finder.get_text()); // Should print the modified text}// 1. Finish the struct definitionpub struct MutableTextFinder<'a> { text: &'a mut String,}// 2. Implement the methods for the structimpl<'a> MutableTextFinder<'a> { pub fn new(text: &'a mut String) -> Self { Self { text } } pub fn find_first(&self, q: &str) -> Option<&str> { self.text.lines().find(|t| t.contains(q)) } pub fn replace_lines(&mut self, q: &str, repl: &'a str) -> Option<&'a str> { *self.text = self .text .lines() .map(|l| if l.contains(q) { repl } else { l }) .collect::<Vec<&str>>() .join("\n"); Some(repl) } pub fn get_text(&self) -> &str { self.text }}// Example usagepub fn main() { let mut text = String::from("Rust is awesome\nLearning Rust\nFun with Rustaceans"); let mut finder = MutableTextFinder::new(&mut text); let first = finder.find_first("Rust"); println!("{:?}", first); // Should print: Some("Rust is awesome") finder.replace_lines("Rust", "Programming in Rust"); println!("{}", finder.get_text()); // Should print the modified text}// 1. Finish the struct definitionpub struct MutableTextFinder<'a> { text: &'a mut String,}// 2. Implement the methods for the structimpl<'a> MutableTextFinder<'a> { pub fn new(text: &'a mut String) -> Self { Self { text } } pub fn find_first(&self, q: &str) -> Option<&str> { self.text.lines().find(|t| t.contains(q)) } pub fn replace_lines(&mut self, q: &str, repl: &'a str) -> Option<&'a str> { *self.text = self .text .lines() .map(|l| if l.contains(q) { repl } else { l }) .collect::<Vec<&str>>() .join("\n"); Some(repl) } pub fn get_text(&self) -> &str { self.text }}// Example usagepub fn main() { let mut text = String::from("Rust is awesome\nLearning Rust\nFun with Rustaceans"); let mut finder = MutableTextFinder::new(&mut text); let first = finder.find_first("Rust"); println!("{:?}", first); // Should print: Some("Rust is awesome") finder.replace_lines("Rust", "Programming in Rust"); println!("{}", finder.get_text()); // Should print the modified text}// 1. Finish the struct definitionpub struct MutableTextFinder<'a> { text: &'a mut String,}// 2. Implement the methods for the structimpl<'a> MutableTextFinder<'a> { pub fn new(text: &'a mut String) -> Self { Self { text } } pub fn find_first(&self, q: &str) -> Option<&str> { self.text.lines().find(|t| t.contains(q)) } pub fn replace_lines(&mut self, q: &str, repl: &'a str) -> Option<&'a str> { *self.text = self .text .lines() .map(|l| if l.contains(q) { repl } else { l }) .collect::<Vec<&str>>() .join("\n"); Some(repl) } pub fn get_text(&self) -> &str { self.text }}// Example usagepub fn main() { let mut text = String::from("Rust is awesome\nLearning Rust\nFun with Rustaceans"); let mut finder = MutableTextFinder::new(&mut text); let first = finder.find_first("Rust"); println!("{:?}", first); // Should print: Some("Rust is awesome") finder.replace_lines("Rust", "Programming in Rust"); println!("{}", finder.get_text()); // Should print the modified text}// 1. Finish the struct definitionpub struct MutableTextFinder<'a>{ mutable_ref : &'a mut String,}// 2. Implement the methods for the structimpl<'a> MutableTextFinder<'a>{ pub fn new(mut_ref : &'a mut String) -> Self{ Self{mutable_ref : mut_ref} } pub fn find_first(&self, text: &str) -> Option<&str>{ self.mutable_ref.lines().find(|line|line.contains(text)) } pub fn replace_lines(&mut self,text : &str, replacement: &str){ let new_lines = self.mutable_ref.lines().map(|line|if line.contains(text){replacement.to_string()}else{line.to_string()}).collect::<Vec<String>>().join("\n"); *self.mutable_ref = new_lines; } pub fn get_text(&self) -> &str{ self.mutable_ref }}// Example usagepub fn main() { let mut text = String::from("Rust is awesome\nLearning Rust\nFun with Rustaceans"); let mut finder = MutableTextFinder::new(&mut text); let first = finder.find_first("Rust"); println!("{:?}", first); // Should print: Some("Rust is awesome") finder.replace_lines("Rust", "Programming in Rust"); println!("{}", finder.get_text()); // Should print the modified text}// 1. Finish the struct definitionpub struct MutableTextFinder<'a> { text: &'a mut String,}impl<'a> MutableTextFinder<'a> { pub fn new(text: &'a mut String) -> Self { MutableTextFinder { text } } pub fn find_first(&self, key: &str) -> Option<&str> { self.text.split("\n").find(|line| line.contains(key)) } pub fn replace_lines(&mut self, key: &str, replacement: &str) { let mut found = false; let new_text = self .text .split('\n') .map(|line| { if line.contains(key) { found = true; replacement } else { line } }) .collect::<Vec<&str>>() .join("\n"); if found { *self.text = new_text; } } pub fn get_text(&self) -> &String { self.text }}// 2. Implement the methods for the struct// Example usagepub fn main() { let mut text = String::from("Rust is awesome\nLearning Rust\nFun with Rustaceans"); let mut finder = MutableTextFinder::new(&mut text); let first = finder.find_first("Rust"); println!("{:?}", first); // Should print: Some("Rust is awesome") finder.replace_lines("Rust", "Programming in Rust"); println!("{}", finder.get_text()); // Should print the modified text}// 1. Finish the struct definitionpub struct MutableTextFinder<'a> { pub text: &'a mut String,}// 2. Implement the methods for the structimpl<'a> MutableTextFinder<'a> { pub fn new(word: &'a mut String) -> Self { MutableTextFinder { text: word, } } pub fn find_first(&self, keyword: &str) -> Option<&str> { if let Some(text) = self.text.lines().find(|line| line.contains(keyword)) { Some(text) } else { None } } pub fn replace_lines(&mut self, keyword: &str, replacement: &str) { let new_text = self .text .lines() .map(|line| { if line.contains(keyword) { line.replace(line, replacement) } else { line.to_string() } }) .collect::<Vec<_>>() .join("\n"); *self.text = new_text; } pub fn get_text(&self) -> &str { self.text }}// Example usagepub fn main() { let mut text = String::from("Rust is awesome\nLearning Rust\nFun with Rustaceans"); let mut finder = MutableTextFinder::new(&mut text); let first = finder.find_first("Rust"); println!("{:?}", first); // Should print: Some("Rust is awesome") finder.replace_lines("Rust", "Programming in Rust"); println!("{}", finder.get_text()); // Should print the modified text}// 1. Finish the struct definitionpub struct MutableTextFinder<'a> { text: &'a mut String,}// 2. Implement the methods for the structimpl<'a> MutableTextFinder<'a> { pub fn new(s: &'a mut String) -> Self { MutableTextFinder { text: s } } pub fn find_first(&self, s: &str) -> Option<&str> { let result= self.text.lines().find(|l| l.contains(s)); result } pub fn replace_lines(&mut self,keyword:&str,r_line:&str) { let text= self.text.lines().map(|line| if line.contains(keyword) { r_line }else { line } ).collect::<Vec<_>>().join("\n"); *self.text = text; } pub fn get_text(&'a self) -> String{ let res= self.text.clone(); return res; }}// Example usagepub fn main() { let mut text = String::from("Rust is awesome\nLearning Rust\nFun with Rustaceans"); let mut finder = MutableTextFinder::new(&mut text); let first = finder.find_first("Rust"); println!("{:?}", first); // Should print: Some("Rust is awesome") finder.replace_lines("Rust", "Programming in Rust"); println!("{:?}", finder.get_text()); // Should print the modified text}pub struct MutableTextFinder<'a> { pub text: &'a mut String,}impl<'a> MutableTextFinder<'a> { pub fn new(text: &'a mut String) -> Self { Self { text } } pub fn find_first(&self, keyword: &str) -> Option<&str> { self.text.lines().find(|l| l.contains(keyword)) } pub fn replace_lines(&mut self, from: &str, to: &str) { let txt: Vec<&str> = self .text .lines() .map(|x| if x.contains(from) { to } else { x }) .collect::<Vec<&str>>(); *self.text = txt.join("\n"); } pub fn get_text(&self) -> &str { self.text }}// Example usagepub fn main() { let mut text = String::from("Rust is awesome\nLearning Rust\nFun with Rustaceans"); let mut finder = MutableTextFinder::new(&mut text); let first = finder.find_first("Rust"); println!("{:?}", first); // Should print: Some("Rust is awesome") finder.replace_lines("Rust", "Programming in Rust"); println!("{}", finder.get_text()); // Should print the modified text}pub struct MutableTextFinder<'a> { pub text: &'a mut String,}impl<'a> MutableTextFinder<'a> { pub fn new(text: &'a mut String) -> Self { Self { text } } pub fn find_first(&self, keyword: &str) -> Option<&str> { self.text.lines().find(|&l| l.contains(keyword)) } pub fn replace_lines(&mut self, original: &str, changed: &str) { *self.text = self.text.lines().map(|l| if l.contains(original) { changed } else { l }).collect::<Vec<&str>>().join("\n"); } pub fn get_text(&self) -> &str { self.text }}// Example usagepub fn main() { let mut text = String::from("Rust is awesome\nLearning Rust\nFun with Rustaceans"); let mut finder = MutableTextFinder::new(&mut text); let first = finder.find_first("Rust"); println!("{:?}", first); // Should print: Some("Rust is awesome") finder.replace_lines("Rust", "Programming in Rust"); println!("{}", finder.get_text()); // Should print the modified text}// 1. Finish the struct definitionpub struct MutableTextFinder<'a> { text: &'a mut String,}impl<'a> MutableTextFinder<'a> { pub fn new(text: &mut String) -> MutableTextFinder { MutableTextFinder { text } } pub fn find_first(&self, keyword: &str) -> Option<&str> { self.text.lines().find(|x| x.contains(keyword)) } pub fn replace_lines(&mut self, from: &str, to: &str) { let txt: Vec<&str> = self .text .lines() .map(|x| if x.contains(from) { to } else { x }) .collect::<Vec<&str>>(); *self.text = txt.join("\n"); } pub fn get_text(&self) -> &str { self.text }}// Example usagepub fn main() { let mut text = String::from("Rust is awesome\nLearning Rust\nFun with Rustaceans"); let mut finder = MutableTextFinder::new(&mut text); let first = finder.find_first("Rust"); println!("{:?}", first); // Should print: Some("Rust is awesome") finder.replace_lines("Rust", "Programming in Rust"); println!("{}", finder.get_text()); // Should print the modified text}// 1. Finish the struct definitionpub struct MutableTextFinder<'a> { text: &'a mut String,}impl<'a> MutableTextFinder<'a> { pub fn new(text: &mut String) -> MutableTextFinder { MutableTextFinder { text } } pub fn find_first(&self, keyword: &str) -> Option<&str> { self.text.lines().find(|x| x.contains(keyword)) } pub fn replace_lines(&mut self, from: &str, to: &str) { let strs = self .text .lines() .map(|x| { if x.contains(from) { to.to_owned() } else { x.to_owned() } }) .collect::<Vec<String>>() .join("\n"); self.text.clear(); self.text.push_str(&strs); } pub fn get_text(&self) -> &str { self.text }}// Example usagepub fn main() { let mut text = String::from("Rust is awesome\nLearning Rust\nFun with Rustaceans"); let mut finder = MutableTextFinder::new(&mut text); let first = finder.find_first("Rust"); println!("{:?}", first); // Should print: Some("Rust is awesome") finder.replace_lines("Rust", "Programming in Rust"); println!("{}", finder.get_text()); // Should print the modified text}// 1. Finish the struct definitionpub struct MutableTextFinder<'a>{ text: &'a mut String,}impl<'a> MutableTextFinder<'a> { pub fn new(text: &'a mut String) -> Self { Self { text } } pub fn find_first(&self, keyword: &str) -> Option<&str> { self.text.lines().find(|line| line.contains(keyword)) } pub fn replace_lines(&mut self, keyword: &str, replacement_text: &str) { let modified_text = self .text .lines() .map(|line| { if line.contains(keyword){ replacement_text.to_string() } else { line.to_string() } }) .collect::<Vec<String>>() .join("\n"); self.text.clear(); self.text.push_str(&modified_text); } pub fn get_text(&self) -> &str { &self.text.as_str() }}// Example usagepub fn main() { let mut text = String::from("Rust is awesome\nLearning Rust\nFun with Rustaceans"); let mut finder = MutableTextFinder::new(&mut text); let first = finder.find_first("Rust"); println!("{:?}", first); // Should print: Some("Rust is awesome") finder.replace_lines("Rust", "Programming in Rust"); println!("{}", finder.get_text()); // Should print the modified text}pub struct MutableTextFinder<'a> { text: &'a mut String,}impl<'a> MutableTextFinder<'a> { pub fn new(text: &'a mut String) -> Self { Self { text } } pub fn find_first(&self, s: &str) -> Option<&str> { self.text.lines().find(|&x| x.contains(s)) } pub fn replace_lines(&mut self, key: &str, s: &str) { let txt: Vec<&str> = self.text .lines() .map(|x| if x.contains(key) { s } else { x }) .collect(); *self.text = txt.join("\n"); } pub fn get_text(&self) -> &str { self.text }}// Example usagepub fn main() { let mut text = String::from("Rust is awesome\nLearning Rust\nFun with Rustaceans"); let mut finder = MutableTextFinder::new(&mut text); let first = finder.find_first("Rust"); println!("{:?}", first); // Should print: Some("Rust is awesome") finder.replace_lines("Rust", "Programming in Rust"); println!("{}", finder.get_text()); // Should print the modified text}pub struct MutableTextFinder<'a> { pub content: &'a mut String}impl<'a> MutableTextFinder<'a> { pub fn new(content: &'a mut String) -> Self { Self { content } } pub fn find_first(&self, pat: &str) -> Option<&str> { self.content.lines().find(|el| el.contains(pat)) } pub fn replace_lines(&mut self, pat: &str, keyword: &str) { let res: Vec<&str> = self.content.lines().map(|line| { if line.contains(pat) { keyword } else { line } }).collect(); *self.content = res.join("\n"); } pub fn get_text(&self) -> &str { self.content }}// Example usagepub fn main() { let mut text = String::from("Rust is awesome\nLearning Rust\nFun with Rustaceans"); let mut finder = MutableTextFinder::new(&mut text); let first = finder.find_first("Rust"); println!("{:?}", first); // Should print: Some("Rust is awesome") finder.replace_lines("Rust", "Programming in Rust"); println!("{}", finder.get_text()); // Should print the modified text}// 1. Finish the struct definitionpub struct MutableTextFinder<'a> { text: &'a mut String,}// 2. Implement the methods for the structimpl <'a> MutableTextFinder<'a> { pub fn new(value: &'a mut String) -> Self { MutableTextFinder { text: value } } pub fn find_first(&self, value: &str) -> Option<&str> { self.text.lines().find(|line| line.contains(&value) ) } pub fn get_text(&self) -> &str { self.text } pub fn replace_lines(&mut self, value: &str, replace: &str) { let new_lines: Vec<&str> = self .text .lines() .map(|line| { if line.contains(value) { replace } else { line } }) .collect(); *self.text = new_lines.join("\n"); }}// Example usagepub fn main() { let mut text = String::from("Rust is awesome\nLearning Rust\nFun with Rustaceans"); let mut finder = MutableTextFinder::new(&mut text); let first = finder.find_first("Rust"); println!("{:?}", first); // Should print: Some("Rust is awesome") finder.replace_lines("Rust", "Programming in Rust"); println!("{}", finder.get_text()); // Should print the modified text}// 1. Finish the struct definitionpub struct MutableTextFinder<'a> { text: &'a mut String,}// 2. Implement the methods for the structimpl <'a> MutableTextFinder<'a> { pub fn new (s: &'a mut String) -> Self { MutableTextFinder{text: s} } pub fn find_first (&self, keyword: &str) -> Option<&str> { self.text.lines() .find(|line| line.contains(&keyword)) } pub fn replace_lines (&mut self, keyword: &'a str, replace_with: &'a str) { let v = self.text.lines() .map(|line| if line.contains(keyword) {replace_with} else {line}) .collect::<Vec<_>>(); *self.text = v.join("\n"); } pub fn get_text (&self) -> &str { self.text }}// Example usagepub fn main() { let mut text = String::from("Rust is awesome\nLearning Rust\nFun with Rustaceans"); let mut finder = MutableTextFinder::new(&mut text); let first = finder.find_first("Rust"); println!("{:?}", first); // Should print: Some("Rust is awesome") finder.replace_lines("Rust", "Programming in Rust"); println!("{}", finder.get_text()); // Should print the modified text}// 1. Finish the struct definitionpub struct MutableTextFinder <'a>{ text: & 'a mut String ,}// 2. Implement the methods for the structimpl <'a> MutableTextFinder <'a> { pub fn new (value:& 'a mut String)->Self { MutableTextFinder{ text: value } } pub fn find_first (&self ,key: &str)-> Option<&str>{ self.text.lines().find(|line| line.contains(key)) } pub fn replace_lines(&mut self, keyword: &'a str, s:&'a str) { let new_line = self.text.lines().map(|x| if x.contains(keyword){ s}else{x}).collect::<Vec<_>>(); *self.text = new_line.join("\n"); } pub fn get_text(& self)->&str{ self.text }}// Example usagepub fn main() { let mut text = String::from("Rust is awesome\nLearning Rust\nFun with Rustaceans"); let mut finder = MutableTextFinder::new(&mut text); let first = finder.find_first("Rust"); println!("{:?}", first); // Should print: Some("Rust is awesome") finder.replace_lines("Rust", "Programming in Rust"); println!("{}", finder.get_text()); // Should print the modified text}// 1. Finish the struct definitionpub struct MutableTextFinder<'a>{ text : &'a mut String,}// 2. Implement the methods for the structimpl<'a> MutableTextFinder<'a> { pub fn new(t: &'a mut String)-> Self{ Self{text : t} } pub fn find_first(&self, s : &'a str) -> Option<&str>{ for line in self.text.lines(){ if line.contains(s){ return Some(line) } } None } pub fn replace_lines(&mut self, keyword: &'a str, s:&'a str) { let new_line = self.text.lines().map(|x| if x.contains(keyword){ s}else{x}).collect::<Vec<_>>(); *self.text = new_line.join("\n"); } pub fn get_text(&self) -> &str{ return self.text } }// Example usagepub fn main() { let mut text = String::from("Rust is awesome\nLearning Rust\nFun with Rustaceans"); let mut finder = MutableTextFinder::new(&mut text); let first = finder.find_first("Rust"); println!("{:?}", first); // Should print: Some("Rust is awesome") finder.replace_lines("Rust", "Programming in Rust"); println!("{}", finder.get_text()); // Should print the modified text}// 1. Finish the struct definitionpub struct MutableTextFinder<'a> { text: &'a mut String,}impl<'a> MutableTextFinder<'a> { pub fn new(text: &'a mut String) -> Self { Self { text } } pub fn find_first(&self, keyword: &str) -> Option<&str> { self.text.lines().find(|line| line.contains(keyword)) } pub fn replace_lines(&mut self, keyword: &str, replacement: &str) { let new_lines: Vec<_> = self .text .lines() .map(|line| { if line.contains(keyword) { replacement } else { line } }) .collect(); *self.text = new_lines.join("\n"); } pub fn get_text(&self) -> &str { self.text }}// 2. Implement the methods for the struct// Example usagepub fn main() { let mut text = String::from("Rust is awesome\nLearning Rust\nFun with Rustaceans"); let mut finder = MutableTextFinder::new(&mut text); let first = finder.find_first("Rust"); println!("{:?}", first); // Should print: Some("Rust is awesome") finder.replace_lines("Rust", "Programming in Rust"); println!("{}", finder.get_text()); // Should print the modified text}// 1. Finish the struct definitionpub struct MutableTextFinder<'a> { text: &'a mut String}// 2. Implement the methods for the structimpl<'a> MutableTextFinder<'a> { pub fn new(s: &'a mut String) -> Self { MutableTextFinder{text: s} } pub fn find_first(&self, search: &str) -> Option<&str> { self.text.lines().find(|l| l.contains(search)) } pub fn replace_lines(&mut self, keyword: &str, replace: &str) { let lines: Vec<_> = self.text.lines().map(|l| {if l.contains(keyword) {replace} else {l} }).collect(); *self.text = lines.join("\n"); } pub fn get_text(&self) -> &String { self.text }}// Example usagepub fn main() { let mut text = String::from("Rust is awesome\nLearning Rust\nFun with Rustaceans"); let mut finder = MutableTextFinder::new(&mut text); let first = finder.find_first("Rust"); println!("{:?}", first); // Should print: Some("Rust is awesome") finder.replace_lines("Rust", "Programming in Rust"); println!("{}", finder.get_text()); // Should print the modified text}// 1. Finish the struct definitionpub struct MutableTextFinder <'a> { txt: &'a mut String}// 2. Implement the methods for the structimpl <'a> MutableTextFinder <'a> { pub fn new(txt: &'a mut String) -> Self { Self{txt} } pub fn find_first(&self, txt: &str) -> Option<&str> { self.txt.lines().find(|l| l.contains(txt)) } pub fn replace_lines(& mut self, find: &str, rep: & str) { let lines: Vec<&str> = self .txt .lines() .map(|line| { if line.contains(find) { return rep; } line }) .collect(); *self.txt = lines.join("\n"); } pub fn get_text(&self) -> &str { self.txt }}// Example usagepub fn main() { let mut text = String::from("Rust is awesome\nLearning Rust\nFun with Rustaceans"); let mut finder = MutableTextFinder::new(&mut text); let first = finder.find_first("Rust"); println!("{:?}", first); // Should print: Some("Rust is awesome") finder.replace_lines("Rust", "Programming in Rust"); println!("{}", finder.get_text()); // Should print the modified text}// 1. Finish the struct definitionpub struct MutableTextFinder<'a> { value: &'a mut String,}impl<'a> MutableTextFinder<'a> { pub fn new(value: &'a mut String) -> Self { MutableTextFinder { value: value } } pub fn find_first(& self, find: &str) -> Option<& str> { let mut lines = self.value.lines(); lines.find(|line| line.contains(find)) } pub fn replace_lines(& mut self, find: &str, rep: & str) { let lines: Vec<&str> = self .value .lines() .map(|line| { if line.contains(find) { return rep; } line }) .collect(); *self.value = lines.join("\n"); } pub fn get_text(&self) -> &str { self.value }}// 2. Implement the methods for the struct// Example usagepub fn main() { let mut text = String::from("Rust is awesome\nLearning Rust\nFun with Rustaceans"); let mut finder = MutableTextFinder::new(&mut text); let first = finder.find_first("Rust"); println!("{:?}", first); // Should print: Some("Rust is awesome") finder.replace_lines("Rust", "Programming in Rust"); println!("{}", finder.get_text()); // Should print the modified text}// 1. Finish the struct definitionpub struct MutableTextFinder<'a>{ pub text:&'a mut String,}// 2. Implement the methods for the structimpl<'a> MutableTextFinder<'a>{ pub fn new(text:&'a mut String)->Self { Self{text} } pub fn find_first(&self, keyword:&str)->Option<&str>{ self.text.lines().find(|line| line.contains(keyword)) } pub fn replace_lines(&mut self, keyword:&str, replacement:&str){ *self.text = self.text .lines() .map(|line| if line.contains(keyword){replacement} else{line}) .collect::<Vec<_>>() .join("\n"); } pub fn get_text(&self)->&str { self.text } }// Example usagepub fn main() { let mut text = String::from("Rust is awesome\nLearning Rust\nFun with Rustaceans"); let mut finder = MutableTextFinder::new(&mut text); let first = finder.find_first("Rust"); println!("{:?}", first); // Should print: Some("Rust is awesome") finder.replace_lines("Rust", "Programming in Rust"); println!("{}", finder.get_text()); // Should print the modified text}// 1. Finish the struct definitionpub struct MutableTextFinder<'a> { pub text: &'a mut String,}// 2. Implement the methods for the structimpl<'a> MutableTextFinder<'a> { pub fn new(text: &'a mut String) -> Self { Self { text } } pub fn find_first(&self, find: &str) -> Option<&str> { self.text.lines().find(|s| s.contains(find)) } pub fn replace_lines(&mut self, find: &str, replace: &str) { *self.text = self.text .lines() .map(|str| if str.contains(find) { replace } else { str }) .collect::<Vec<_>>() .join("\n"); } pub fn get_text(&self) -> &str { self.text }}// Example usagepub fn main() { let mut text = String::from("Rust is awesome\nLearning Rust\nFun with Rustaceans"); let mut finder = MutableTextFinder::new(&mut text); let first = finder.find_first("Rust"); println!("{:?}", first); // Should print: Some("Rust is awesome") finder.replace_lines("Rust", "Programming in Rust"); println!("{}", finder.get_text()); // Should print the modified text}// 1. Finish the struct definitionpub struct MutableTextFinder<'a> { text: &'a mut String,}// 2. Implement the methods for the structimpl<'a> MutableTextFinder<'a> { pub fn new(text: &'a mut String) -> Self { Self { text } } pub fn find_first(&self, keyword: &str) -> Option<&str> { self.text.lines().find(|line| line.contains(keyword)) } pub fn replace_lines(&mut self, keyword: &str, replacement: &str) { let lines = self .text .lines() .map(|line| { if line.contains(keyword) { replacement } else { line } }) .map(|s| s.to_string()) .collect::<Vec<String>>(); self.text.clear(); self.text.push_str(&lines.join("\n")); } pub fn get_text(&self) -> &str { self.text }}// Example usagepub fn main() { let mut text = String::from("Rust is awesome\nLearning Rust\nFun with Rustaceans"); let mut finder = MutableTextFinder::new(&mut text); let first = finder.find_first("Rust"); println!("{:?}", first); // Should print: Some("Rust is awesome") finder.replace_lines("Rust", "Programming in Rust"); println!("{}", finder.get_text()); // Should print the modified text}pub struct MutableTextFinder<'a> { text: &'a mut String,}impl<'a> MutableTextFinder<'a> { pub fn new(text: &'a mut String) -> Self { Self { text } } pub fn find_first(&self, keyword: &str) -> Option<&str> { self.text.lines().find(|line| line.contains(keyword)) } pub fn replace_lines(&mut self, keyword: &str, replacement: &str) { let lines = self .text .lines() .map(|line| { if line.contains(keyword) { replacement } else { line } }) .map(|s| s.to_string()) .collect::<Vec<String>>(); self.text.clear(); self.text.push_str(&lines.join("\n")); } pub fn get_text(&self) -> &str { self.text }}// 1. Finish the struct definitionpub struct MutableTextFinder<'a>{ base_text: &'a mut String,}// 2. Implement the methods for the structimpl<'a> MutableTextFinder<'a> { pub fn new(base_text: &'a mut String) -> MutableTextFinder{ MutableTextFinder { base_text } } pub fn find_first(&self, keyword: &str) -> Option<&str> { self.base_text.lines().find(|&t| t.contains(keyword)) } pub fn replace_lines(&mut self, keyword: &str, replacement: &str) { *self.base_text = self.base_text.lines() .map(|t| if t.contains(keyword) { replacement }else{ t }) .collect::<Vec<_>>() .join("\n") ; } pub fn get_text(&self) -> &str { self.base_text }}// Example usagepub fn main() { let mut text = String::from("Rust is awesome\nLearning Rust\nFun with Rustaceans"); let mut finder = MutableTextFinder::new(&mut text); let first = finder.find_first("Rust"); println!("{:?}", first); // Should print: Some("Rust is awesome") finder.replace_lines("Rust", "Programming in Rust"); println!("{}", finder.get_text()); // Should print the modified text}// 1. Finish the struct definitionpub struct MutableTextFinder<'a> { data: &'a mut String,}// 2. Implement the methods for the structimpl<'a> MutableTextFinder<'a> { pub fn new(text: &'a mut String) -> Self { Self {data: text} } pub fn find_first(&self, s: &str) -> Option<&str> { self.data.lines().find(|l| l.contains(s)) } pub fn replace_lines(&mut self, s: &str, t: &str) { *self.data = self.data.lines().map(|l| { if l.contains(s) {t}else { l } }) .collect::<Vec<_>>() .join("\n"); } pub fn get_text(&self) -> &str { self.data }}// Example usagepub fn main() { let mut text = String::from("Rust is awesome\nLearning Rust\nFun with Rustaceans"); let mut finder = MutableTextFinder::new(&mut text); let first = finder.find_first("Rust"); println!("{:?}", first); // Should print: Some("Rust is awesome") finder.replace_lines("Rust", "Programming in Rust"); println!("{}", finder.get_text()); // Should print the modified text}//// _oo0oo_// o8888888o// 88" . "88// (| -_- |)// 0\ = /0// ___/`---'\___// .' \\| |// '.// / \\||| : |||// \// / _||||| -:- |||||- \// | | \\\ - /// | |// | \_| ''\---/'' |_/ |// \ .-\__ '-' ___/-. /// ___'. .' /--.--\ `. .'___// ."" '< `.___\_<|>_/___.' >' "".// | | : `- \`.;`\ _ /`;.`/ - ` : | |// \ \ `_. \_ __\ /__ _/ .-` / /// =====`-.____`.___ \_____/___.-`___.-'=====// `=---='////// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//Phật độ hộ trì Hoàng Huỳnh ĐẸP TRAI | code không bao giờ BUG// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~// 1. Finish the struct definitionpub trait MutableTextFinderTrait<'a> { fn new(str: &'a mut String) -> MutableTextFinder<'a>; fn find_first(&self, word: &str) -> Option<&str>; fn replace_lines(&mut self, find_word: &str, word: &str) -> (); fn get_text(&self) -> String;}pub struct MutableTextFinder<'a> { pub slices: &'a mut String,}// 2. Implement the methods for the structimpl<'a> MutableTextFinderTrait<'a> for MutableTextFinder<'a> { fn new(str: &'a mut String) -> MutableTextFinder<'a> { Self { slices: str, } } fn find_first(&self, word: &str) -> Option<&str> { self.slices.lines().find(|l| l.contains(word)) } fn replace_lines(&mut self, find_word: &str, word: &str) -> () { println!("find_word: {}", find_word); println!("word: {}", word); let new_text = self.slices .lines() .map(|l| if l.contains(find_word) { word.to_string() } else { l.to_string() }) .into_iter() .collect::<Vec<String>>() // collect into a Vec first .join("\n"); // then join *self.slices = new_text; } fn get_text(&self) -> String { self.slices.to_string() }}// Example usagepub fn main() { let mut text = String::from("Rust is awesome\nLearning Rust\nFun with Rustaceans"); let mut finder = MutableTextFinder::new(&mut text); let first = finder.find_first("Rust"); println!("{:?}", first); // Should print: Some("Rust is awesome") finder.replace_lines("Rust", "Programming in Rust"); println!("{}", finder.get_text()); // Should print the modified text}// 1. Finish the struct definitionpub struct MutableTextFinder<'a>{ pub text: &'a mut String}impl<'a> MutableTextFinder<'a>{ pub fn new(s: &'a mut String) -> Self { Self{text: s} } pub fn find_first(&self, s: &str) ->Option<&str> { self.text.lines().find_map(|x| if x.contains(s) {Some(x)} else {None}) } pub fn replace_lines(&mut self, s: &str, t: &str) { *self.text = self.text.lines().map(|old_line| if old_line.contains(s) {t} else {old_line}).collect::<Vec<_>>().join("\n"); } pub fn get_text(&self) -> &str { self.text }}// Example usagepub fn main() { let mut text = String::from("Rust is awesome\nLearning Rust\nFun with Rustaceans"); let mut finder = MutableTextFinder::new(&mut text); let first = finder.find_first("Rust"); println!("{:?}", first); // Should print: Some("Rust is awesome") finder.replace_lines("Rust", "Programming in Rust"); println!("{}", finder.get_text()); // Should print the modified text}// 0. Finish the struct definitionpub struct MutableTextFinder<'a> { text: &'a mut String,}// 2. Implement the methods for the structimpl<'a> MutableTextFinder<'a> { pub fn new(text: &'a mut String) -> Self { Self { text } } pub fn find_first(&self, key: &str) -> Option<&str> { self.text.lines().find(|line| line.contains(key)) } pub fn replace_lines(&mut self, key: &str, new_line: &str) { *self.text = self.text.lines().map(|old_line| if old_line.contains(key) {new_line} else {old_line}).collect::<Vec<_>>().join("\n"); } pub fn get_text(&self) -> &str { self.text }}// Example usagepub fn main() { let mut text = String::from("Rust is awesome\nLearning Rust\nFun with Rustaceans"); let mut finder = MutableTextFinder::new(&mut text); let first = finder.find_first("Rust"); println!("{:?}", first); // Should print: Some("Rust is awesome") finder.replace_lines("Rust", "Programming in Rust"); println!("{}", finder.get_text()); // Should print the modified text}// 1. Finish the struct definitionpub struct MutableTextFinder<'a>(&'a mut String);// 2. Implement the methods for the structimpl<'a> MutableTextFinder<'a> { pub fn new(text: &'a mut String) -> Self { Self(text) } pub fn find_first(&self, keyword: &str) -> Option<&str> { self.0.lines().find(|line| line.contains(keyword)) } pub fn replace_lines(&mut self, keyword: &str, new_text: &str ) { *self.0 = self.0.lines().map(|line| { if line.contains(keyword) { new_text } else { line } }) .collect::<Vec<_>>() .join("\n") } pub fn get_text(&self) -> &String { self.0 }}// Example usagepub fn main() { let mut text = String::from("Rust is awesome\nLearning Rust\nFun with Rustaceans"); let mut finder = MutableTextFinder::new(&mut text); let first = finder.find_first("Rust"); println!("{:?}", first); // Should print: Some("Rust is awesome") finder.replace_lines("Rust", "Programming in Rust"); println!("{}", finder.get_text()); // Should print the modified text}// 1. Finish the struct definitionpub struct MutableTextFinder<'a>(&'a mut String);// 2. Implement the methods for the structimpl<'a> MutableTextFinder<'a> { pub fn new(text: &'a mut String) -> Self { Self(text) } pub fn find_first(&self, keyword: &str) -> Option<&str> { self.0.lines().find(|line| line.contains(keyword)) } pub fn replace_lines(&mut self, keyword: &str, replacement: &str) { *self.0 = self .0 .split("\n") .map(|line| { if line.contains(keyword) { replacement } else { line } }) .collect::<Vec<&str>>() .join("\n") } pub fn get_text(&self) -> &String { &self.0 }}// Example usagepub fn main() { let mut text = String::from("Rust is awesome\nLearning Rust\nFun with Rustaceans"); let mut finder = MutableTextFinder::new(&mut text); let first = finder.find_first("Rust"); println!("{:?}", first); // Should print: Some("Rust is awesome") finder.replace_lines("Rust", "Programming in Rust"); println!("{}", finder.get_text()); // Should print the modified text}pub struct MutableTextFinder<'a>(&'a mut String);impl<'a> MutableTextFinder<'a> { pub fn new(text: &'a mut String) -> MutableTextFinder<'a> { MutableTextFinder(text) } pub fn find_first(&'a self, pattern: &str) -> Option<&'a str> { self.0.lines().find(|x| x.contains(pattern)) } pub fn replace_lines(&mut self, repl: &str, val: &str) { *self.0 = self .0 .lines() .map(|x| { if x.contains(repl) { val } else { x } }) .collect::<Vec<_>>() .join("\n"); } pub fn get_text(&'a self) -> &str { &self.0 }}// 2. Implement the methods for the struct// Example usagepub fn main() { let mut text = String::from("Rust is awesome\nLearning Rust\nFun with Rustaceans"); let mut finder = MutableTextFinder::new(&mut text); let first = finder.find_first("Rust"); println!("{:?}", first); // Should print: Some("Rust is awesome") finder.replace_lines("Rust", "Programming in Rust"); println!("{}", finder.get_text()); // Should print the modified text}// 1. Finish the struct definitionpub struct MutableTextFinder<'a> { text: &'a mut String}// 2. Implement the struct and define the methodsimpl<'a> MutableTextFinder<'a> { pub fn new(text: &'a mut String) -> Self { Self { text } } pub fn find_first(&'a self, word: &str) -> Option<&'a str> { for l in self.text.lines() { if l.contains(&word) { return Some(&l); } } return None; } pub fn replace_lines(&mut self, word: &str, replacement: &str) { *self.text = self.text .lines() .map(|l| if l.contains(word) { replacement } else { l }) .collect::<Vec<_>>() .join("\n"); } pub fn get_text(&'a self) -> &str { &self.text }}// Example usagepub fn main() { let mut text = String::from("Rust is awesome\nLearning Rust\nFun with Rustaceans"); let mut finder = MutableTextFinder::new(&mut text); let first = finder.find_first("Rust"); println!("{:?}", first); // Should print: Some("Rust is awesome") finder.replace_lines("Rust", "Programming in Rust"); println!("{}", finder.get_text()); // Should print the modified text}// 1. Finish the struct definitionpub struct MutableTextFinder<'a>(&'a mut String);// 2. Implement the methods for the structimpl<'a> MutableTextFinder<'a> { pub fn new(text: &'a mut String) -> Self { MutableTextFinder(text) } pub fn find_first(&self, target: &str) -> Option<&str> { self.0.lines().find(|line| line.contains(target)) } pub fn replace_lines(&mut self, target: &str, with: &str) { *self.0 = self.0 .lines() .map(|line| if line.contains(target) { with } else { line }) .collect::<Vec<_>>() .join("\n"); } pub fn get_text(&self) -> &str { self.0 }}// Example usagepub fn main() { let mut text = String::from("Rust is awesome\nLearning Rust\nFun with Rustaceans"); let mut finder = MutableTextFinder::new(&mut text); let first = finder.find_first("Rust"); println!("{:?}", first); // Should print: Some("Rust is awesome") finder.replace_lines("Rust", "Programming in Rust"); println!("{}", finder.get_text()); // Should print the modified text}// 1. Finish the struct definitionpub struct MutableTextFinder<'a> { pub text: &'a mut String,}// 2. Implement the methods for the structimpl<'a> MutableTextFinder<'a> { pub fn new(text: &'a mut String) -> MutableTextFinder { MutableTextFinder { text } } pub fn find_first(&self, keyword: &str) -> Option<&str> { self.text.lines().filter(|line| line.contains(keyword)).next() } pub fn replace_lines(&mut self, keyword: &str, replacement: &str) { *self.text = self.text.lines().map(|line| { if line.contains(keyword) { return replacement; } line }).collect::<Vec<_>>().join("\n"); } pub fn get_text(&self) -> &str { self.text }}// Example usagepub fn main() { let mut text = String::from("Rust is awesome\nLearning Rust\nFun with Rustaceans"); let mut finder = MutableTextFinder::new(&mut text); let first = finder.find_first("Rust"); println!("{:?}", first); // Should print: Some("Rust is awesome") finder.replace_lines("Rust", "Programming in Rust"); println!("{}", finder.get_text()); // Should print the modified text}// 1. Finish the struct definitionpub struct MutableTextFinder<'a> { text: &'a mut String}// 2. Implement the methods for the structimpl<'a> MutableTextFinder<'a> { pub fn new(content: &'a mut String) -> Self { Self { text: content } } pub fn get_text(&self) -> &String { self.text } pub fn find_first(&self, keyword: &str) -> Option<&str>{ self.text.lines().find(|elm| elm.contains(keyword)) } pub fn replace_lines(&mut self, keyword: &str, replacement: &'a str) { *self.text = self.text.lines().map(|line| if line.contains(keyword) {replacement} else {line}).collect::<Vec<_>>().join("\n"); }}// Example usagepub fn main() { let mut text = String::from("Rust is awesome\nLearning Rust\nFun with Rustaceans"); let mut finder = MutableTextFinder::new(&mut text); let first = finder.find_first("Rust"); println!("{:?}", first); // Should print: Some("Rust is awesome") finder.replace_lines("Rust", "Programming in Rust"); println!("{}", finder.get_text()); // Should print the modified text}// 1. Finish the struct definitionpub struct MutableTextFinder<'a> { pub text: &'a mut String}impl<'a> MutableTextFinder<'a> { pub fn new(text: &'a mut String) -> MutableTextFinder<'a> { Self {text} } pub fn find_first(&self, keyword: &str) -> Option<&str> { self.text.lines().filter(|s| s.contains(keyword)).next() } pub fn replace_lines(&mut self, keyword: &str, repl: &str) { let repl_text = self.text .lines() .map(|s| { if s.contains(keyword) { repl } else { s } }) .collect::<Vec<_>>() .join("\n"); self.text.clear(); self.text.push_str(&repl_text); } pub fn get_text(&self) -> &str { self.text }}// 2. Implement the methods for the struct// Example usagepub fn main() { let mut text = String::from("Rust is awesome\nLearning Rust\nFun with Rustaceans"); let mut finder = MutableTextFinder::new(&mut text); let first = finder.find_first("Rust"); println!("{:?}", first); // Should print: Some("Rust is awesome") finder.replace_lines("Rust", "Programming in Rust"); println!("{}", finder.get_text()); // Should print the modified text}// 1. Finish the struct definitionpub struct MutableTextFinder<'a> { text: &'a mut String,}// 2. Implement the methods for the structimpl<'a> MutableTextFinder<'a> { pub fn new(string: &'a mut String) -> Self { Self { text: string } } pub fn find_first(&self, keyword: &str) -> Option<&str> { self.text.lines().find(|line| line.contains(keyword)) } pub fn replace_lines(&mut self, from: &str, to: &str) { *self.text = self.text.lines().map(|line| { if line.contains(from) { to } else { line } }).collect::<Vec<&str>>().join("\n") } pub fn get_text(&self) -> &str { &self.text }}// Example usagepub fn main() { let mut text = String::from("Rust is awesome\nLearning Rust\nFun with Rustaceans"); let mut finder = MutableTextFinder::new(&mut text); let first = finder.find_first("Rust"); println!("{:?}", first); // Should print: Some("Rust is awesome") finder.replace_lines("Rust", "Programming in Rust"); println!("{}", finder.get_text()); // Should print the modified text}// 1. Finish the struct definitionpub struct MutableTextFinder<'a>(&'a mut String);// 2. Implement the methods for the structimpl<'a> MutableTextFinder<'a> { pub fn new(text: &'a mut String) -> Self { Self(text) } pub fn find_first(&self, needle: &str) -> Option<&str> { self.0.lines().find(|line| line.contains(needle)) } pub fn replace_lines(&mut self, needle: &str, replacement: &str) { *self.0 = self .0 .lines() .map(|line| { if line.contains(needle) { replacement.to_string() } else { line.to_string() } }) .collect::<Vec<String>>() .join("\n"); } pub fn get_text(&self) -> &str { self.0 }}// Example usagepub fn main() { let mut text = String::from("Rust is awesome\nLearning Rust\nFun with Rustaceans"); let mut finder = MutableTextFinder::new(&mut text); let first = finder.find_first("Rust"); println!("{:?}", first); // Should print: Some("Rust is awesome") finder.replace_lines("Rust", "Programming in Rust"); println!("{}", finder.get_text()); // Should print the modified text}// 1. Define the structpub struct MutableTextFinder<'a> { text: &'a mut String,}impl<'a> MutableTextFinder<'a> { pub fn new(text: &'a mut String) -> Self { Self { text } } pub fn find_first(&self, key: &str) -> Option<&str> { self.text.lines().find(|spl| spl.contains(key)) } pub fn replace_lines(&mut self, from: &str, to: &str) { *self.text = self .text .lines() .map(|line| if line.contains(from) { to } else { line }) .collect::<Vec<&str>>() .join("\n"); } pub fn get_text(&self) -> &str { &self.text }}// 2. Implement the methods for the struct// Example usagepub fn main() { let mut text = String::from("Rust is awesome\nLearning Rust\nFun with Rustaceans"); let mut finder = MutableTextFinder::new(&mut text); let first = finder.find_first("Rust"); println!("{:?}", first); // Should print: Some("Rust is awesome") finder.replace_lines("Rust", "Programming in Rust"); println!("{}", finder.get_text()); // Should print the modified text}pub struct MutableTextFinder<'a> { text: &'a mut String,}impl<'a> MutableTextFinder<'a> { pub fn new(text: &'a mut String) -> Self { Self { text } } pub fn find_first(&self, reference: &str) -> Option<&str> { self.text .lines() .find(|line| line.contains(reference)) } pub fn replace_lines(&mut self, reference: &str, replace: &str) { *self.text = self.text .lines() .map(|line| { if line.contains(reference) { replace } else { line } }).collect::<Vec<&str>>() .join("\n"); } pub fn get_text(&self) -> &String { self.text }}pub fn main() { let mut text = String::from("Rust is awesome\nLearning Rust\nFun with Rustaceans"); let mut finder = MutableTextFinder::new(&mut text); let first = finder.find_first("Rust"); println!("{:?}", first); // Should print: Some("Rust is awesome") finder.replace_lines("Rust", "Programming in Rust"); println!("{}", finder.get_text()); // Should print the modified text}// 1. Finish the struct definitionpub struct MutableTextFinder<'a>{ text: &'a mut String,}// 2. Implement the methods for the structimpl<'a> MutableTextFinder<'a> { pub fn new(text: &'a mut String) -> Self { Self { text: text } } pub fn find_first(&self, keyword: &str) -> Option<&str> { self.text.lines().find(|line| line.contains(keyword)) } pub fn replace_lines(&mut self, keyword: &str, replace_line: &str) { *self.text = self .text .lines() .map(|line| { if line.contains(keyword) { replace_line } else { line } }) .collect::<Vec<&str>>() .join("\n"); } pub fn get_text(&self) -> &str { &self.text }}// Example usagepub fn main() { let mut text = String::from("Rust is awesome\nLearning Rust\nFun with Rustaceans"); let mut finder = MutableTextFinder::new(&mut text); let first = finder.find_first("Rust"); println!("{:?}", first); // Should print: Some("Rust is awesome") finder.replace_lines("Rust", "Programming in Rust"); println!("{}", finder.get_text()); // Should print the modified text}// 1. Finish the struct definitionpub struct MutableTextFinder<'a>{ text: &'a mut String,}// 2. Implement the methods for the structimpl<'a> MutableTextFinder<'a> { pub fn new(text: &'a mut String) -> Self { Self { text: text } } pub fn find_first(&self, keyword: &str) -> Option<&str> { self.text.lines().find(|line| line.contains(keyword)) } pub fn replace_lines(&mut self, keyword: &str, replace_line: &str) { *self.text = self .text .lines() .map(|line| { if line.contains(keyword) { replace_line } else { line } }) .collect::<Vec<&str>>() .join("\n"); } pub fn get_text(&self) -> &str { &self.text }}// Example usagepub fn main() { let mut text = String::from("Rust is awesome\nLearning Rust\nFun with Rustaceans"); let mut finder = MutableTextFinder::new(&mut text); let first = finder.find_first("Rust"); println!("{:?}", first); // Should print: Some("Rust is awesome") finder.replace_lines("Rust", "Programming in Rust"); println!("{}", finder.get_text()); // Should print the modified text}