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> { text: &'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, input: &str) -> Option<&str> { for x in self.text.lines() { if x.contains(input) { return Some(x); } } return None; } pub fn replace_lines(&mut self, pattern: &str, input: &str) { let mut vec: Vec<String> = Vec::new(); for line in self.text.lines() { if line.contains(pattern) { vec.push(input.to_string()); } else { vec.push(line.to_string()); } } *self.text = vec.into_iter().collect::<Vec<_>>().join("\n"); } pub fn get_text(&self) -> String { self.text.clone() }}// 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>{ string: &'a mut String}// 2. Implement the methods for the structimpl<'a> MutableTextFinder <'a>{ pub fn new(input: &'a mut String) -> Self { Self { string: input } } pub fn find_first(&self, keyword: &str) -> Option<&str> { self.string.split("\n").find(|x| x.contains(keyword)) } pub fn replace_lines(&mut self, keyword: &str, replacement: &str) { *self.string = self.string.split("\n").map(|x| if x.contains(keyword) {replacement.to_string()} else {x.to_string()}).collect::<Vec<_>>().join("\n"); } pub fn get_text(&self) -> &String { return self.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> { 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.split("\n").find(move |l| l.contains(q)) } pub fn replace_lines(&mut self, q: &str, replacement: &str) { *self.text = self.text.split("\n") .map(|l| if l.contains(q) { replacement.to_string() } else { l.to_string() }) .collect::<Vec<_>>() .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> { 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, pattern: &str) -> Option<&str> { self.text.lines().find(|line| line.contains(pattern)) } pub fn replace_lines(&mut self, old: &str, new: &str) { *self.text = self .text .lines() .map(|line| if line.contains(old) { new.to_string() } else { line.to_string() }) .collect::<Vec<String>>() .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, substr: &str) -> Option<&str> { self.text.lines().find(|l| l.contains(substr)) } pub fn replace_lines(&mut self, substr: &str, replacement: &str) { let nt = self .text .lines() .map(|l| if l.contains(substr) { replacement } else { l }) .collect::<Vec<_>>() .join("\n"); *self.text = nt; } pub fn get_text(&self) -> &str { &self.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, substr: &str) -> Option<&str> { self.text.lines().find(|l| l.contains(substr)) } pub fn replace_lines(&mut self, substr: &str, replacement: &str) { let nt = self .text .lines() .map(|l| if l.contains(substr) { replacement } else { l }) .collect::<Vec<_>>() .join("\n"); *self.text = nt; } pub fn get_text(&self) -> &str { &self.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, substr: &str) -> Option<&str> { self.0.lines().find(|l| l.contains(substr)) } pub fn replace_lines(&mut self, substr: &str, replacement: &str) { let nt = self.0.lines() .map(|l| if l.contains(substr) { replacement } else { l }) .collect::<Vec<_>>() .join("\n"); *self.0 = nt; } 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}
pub struct MutableTextFinder<'a>(&'a mut String);impl<'a> MutableTextFinder<'a> { pub fn new(text: &'a mut String) -> Self { MutableTextFinder(text) } pub fn find_first(&self, sub_string: &str) -> Option<&str> { self.0.lines().find(|line| line.contains(sub_string)) } pub fn replace_lines(&mut self, sub_string: &str, replacement_line_string: &str) { let new_text: String = self.0 .lines() .map(|line| { if line.contains(sub_string) { replacement_line_string } else { line } }) .collect::<Vec<_>>() .join("\n"); *self.0 = new_text; } 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) -> Self { Self {text} } pub fn find_first(&self, search: &str) -> Option<&str> { for line in self.text.lines() { if line.contains(search) { return Some(line); } } return None; } pub fn replace_lines(&mut self, search: &str, replace: &str) { let new_text = &self.text.lines().map(|line| { if line.contains(search) { replace } else { line } }).collect::<Vec<&str>>().join("\n"); self.text.clear(); self.text.push_str(new_text); } 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> { pub text: &'a mut String,}// 2. Implement the methods for the structimpl<'a> MutableTextFinder<'a> { pub fn new(text: &'a mut String) -> Self { MutableTextFinder { 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, replacement: &str) { let text = self.text .lines() .map(|l| if l.contains(keyword) { replacement.to_string() } else { l.to_string() }) .collect::<Vec<_>>() .join("\n"); *self.text = text; } 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. Define the struct// pub struct TextFinder<'a> {// text: &'a str// }// impl<'a> TextFinder<'a> {// pub fn new(text: &'a str) -> Self {// TextFinder { text }// }// pub fn find_first<'b>(&self, line: &'b str) -> Option<&'b str> where 'a : 'b {// let mut lines = self.text.lines(); // let item = lines.find(|&x| x.contains(line)); // item// }// pub fn find_many<'b>(&self, line: &'b str) -> Vec<&'b str> where 'a : 'b {// let filtered = self.text.lines().into_iter().filter(|x| x.contains(line)).collect();// filtered// }// }// // 2. Implement the struct and define the methods// // Example usage// pub fn main() {// let text = "Rust is fast and memory-efficient.\nOwnership is key to Rust's safety.\nRustaceans love the borrow checker.";// let finder = TextFinder::new(text);// let first = finder.find_first("Rust");// println!("{:?}", first); // Should print: Some("Rust is fast and memory-efficient.")// let matches = finder.find_many("Rust");// println!("{:?}", matches); // Should print: ["Rust is fast and memory-efficient.", "Ownership is key to Rust's safety."]// }// 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 { MutableTextFinder{text: text} } pub fn find_first<'b>(&'b self, line: &'b str) -> Option<&'b str> where 'a : 'b { let mut lines = self.text.lines(); let item = lines.find(|&x| x.contains(line)); item } pub fn replace_lines<'b>(&'b mut self, line: &'b str, refLine: &'b str) { let text = self.text .lines() .map(|l| if l.contains(line) { refLine.to_string() } else { l.to_string() }) .collect::<Vec<String>>() .join("\n"); *self.text = text; } pub fn get_text<'b> (&'b self) -> &'b String where 'a : 'b { 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) -> MutableTextFinder { MutableTextFinder { text } } pub fn find_first(&self, needle: &str) -> Option<&str> { self.text.lines().find(|l| l.contains(needle)) } pub fn replace_lines(&mut self, needle: &str, keyword: &'a str) { let text = self.text .lines() .map(|l| if l.contains(needle) { keyword.to_string() } else { l.to_string() }) .collect::<Vec<String>>() .join("\n"); *self.text = text; } 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>(pub &'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, pattern: &str) -> Option<&str> { self.0.lines().find(|line| line.contains(pattern)) } pub fn replace_lines(&mut self, pattern: &str, replacement: &str) { for (start, len) in line_ranges(self.0).into_iter().rev() { if self.0[start..start + len].contains(pattern) { self.0.replace_range(start..start + len, replacement) } } } pub fn get_text(&self) -> &String { self.0 }}fn line_ranges(text: &str) -> Vec<(usize, usize)> { text.split('\n').map(|line| (0, line.len())).scan(0, |prev_end, (start, len)| { let new_start = start + *prev_end; *prev_end = new_start + len + 1; Some((new_start, len)) }).collect()}// 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, pattern: &str) -> Option<&str> { self.text .lines() .find(|line| line.contains(pattern)) } pub fn replace_lines(&mut self, pattern: &str, replacement: &str) { let new_text: String = self.text .lines() .map(|line| { if line.contains(pattern) { replacement.to_string() } else { line.to_string() } }) .collect::<Vec<String>>() .join("\n"); *self.text = new_text; } 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> { 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, word: &'a str) -> Option<&str> { self.text.lines().find(|x| x.contains(word)) } pub fn replace_lines(&mut self, from: &str, to: &str) { *self.text = self .text .lines() .filter_map(|line| { if line.contains(from) { Some(to) } else { Some(line) } }) .collect::<Vec<_>>() .join("\n"); } pub fn get_text(&self) -> &str { self.text.as_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> { pub content: &'a mut String,}// 2. Implement the methods for the structimpl<'a> MutableTextFinder<'a> { pub fn new(input: &'a mut String) -> Self { Self { content: input } } pub fn find_first(&self, kwd: &str) -> Option<&str> { let lines: Vec<&str> = self.content.split('\n').collect(); for line in lines { if line.contains(&kwd) { return Some(line); } } None } pub fn replace_lines(&mut self, target: &str, new: &str) { let new_text = self.content.lines() .map(|ln| if ln.contains(target) { new } else { ln }) .collect::<Vec<_>>().join("\n"); *self.content = new_text; } 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(text: &'a mut String) -> Self { Self { text } } pub fn find_first(&self, word: &str) -> Option<&str> { self.text.lines().find(|&x| x.contains(word)) } pub fn replace_lines(&mut self, from: &str, to: &str) { *self.text = self .text .lines() .filter_map(|line| { if line.contains(from) { Some(to) } else { Some(line) } }) .collect::<Vec<_>>() .join("\n"); } pub fn get_text(&self) -> &str { self.text.as_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,}// 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, text: &'a str) -> Option<&str> { self.text.lines().find(|&x| x.contains(text)) } pub fn get_text(&self) -> &String { self.text } pub fn replace_lines(&mut self, from: &'a str, to: &'a str) { *self.text = self .text .lines() .filter_map(|line| { if line.contains(from) { Some(to) } else { Some(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> { 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, text: &'a str) -> Option<&str> { self.text.lines().find(|&x| x.contains(text)) } pub fn get_text(&self) -> String { self.text.to_owned() } pub fn replace_lines(&mut self, from: &'a str, to: &'a str) { *self.text = self .text .lines() .filter_map(|line| { if line.contains(from) { Some(to) } else { Some(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> { 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, text: &'a str) -> Option<&str> { self.text.lines().find(|&x| x.contains(text)) } pub fn get_text(&self) -> String { self.text.to_owned() } pub fn replace_lines(&mut self, from: &'a str, to: &'a str) { *self.text = self .text .lines() .map(|line| { if line.contains(from) { to.to_string() } else { line.to_string() } }) .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> { text: &'a mut String,}// 2. Implement the methods for the structimpl<'a> MutableTextFinder<'a> { pub fn new(text: &'a mut String) -> MutableTextFinder<'a> { MutableTextFinder { text } } pub fn get_text(&self) -> &String { &self.text } pub fn find_first(&self, keyword: &str) -> Option<&str> { self.text.lines().find(|ln| ln.contains(keyword)) } pub fn replace_lines(&mut self, keyword: &str, replacement: &str) { let new_text = self.text.lines() .map(|ln| if ln.contains(keyword) { replacement } else { ln }) .collect::<Vec<_>>().join("\n"); *self.text = new_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 content: &'a mut String,}// 2. Implement the methods for the structimpl<'a> MutableTextFinder<'a> { pub fn new(sl: &'a mut String) -> Self { Self { content: sl, } } pub fn find_first(&self, keyword: &str) -> Option<&str> { for line in self.content.split('\n') { if line.contains(keyword) { return Some(line); } } None } pub fn replace_lines(&mut self, keyword: &str, replacement_str: &str) { let new_content = self.content.split('\n').map(|line| { if line.contains(keyword) { replacement_str } else { line } }).collect::<Vec<&str>>().join("\n"); *self.content = new_content; } 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}
pub struct MutableTextFinder<'a> { text: &'a mut String,}// 2. Implement the methods for the structimpl<'a> MutableTextFinder<'a> { // Constructor pub fn new(text: &'a mut String) -> Self { Self { text } } // Method to find the first occurrence of a substring pub fn find_first(&self, substring: &str) -> Option<&str> { self.text.lines().find(|line| line.contains(substring)) } // Method to replace lines containing a substring pub fn replace_lines(&mut self, substring: &str, replacement: &str) { *self.text = self.text.lines().filter_map(|line| { if line.contains(substring) { Some(replacement.to_string()) } else { Some(line.to_string()) } }).collect::<Vec<String>>().join("\n"); } // Method to get the text 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}
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, query: &str) -> Option<&str> { self.text.lines().find(|l| l.contains(query)) } pub fn replace_lines(&mut self, query: &str, replace: &str) { *self.text = self .text .lines() .filter_map(|l| { if l.contains(query) { Some(replace) } else { Some(l) } }) .collect::<Vec<_>>() .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> { pub text: &'a mut String,}// 2. Implement the methods for the structimpl<'a> MutableTextFinder<'a> { pub fn new(txt: &'a mut String) -> Self{ Self { text: txt} } pub fn find_first(&self, k: &str)-> Option<&str> { for line in self.text.lines() { if line.contains(k) { return Some(line); } } None } pub fn replace_lines(&mut self, k: &str, v:&str) { let mut new_text = String::new(); for line in self.text.lines() { if line.contains(k) { new_text.push_str(v); }else{ new_text.push_str(line); } new_text.push('\n'); } if !self.text.ends_with("\n") { new_text.pop(); } *self.text = new_text; } 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> { pub text: &'a mut String,}// 2. Implement the methods for the structimpl<'a> MutableTextFinder<'a> { pub fn new(txt: &'a mut String) -> Self{ Self { text: txt} } pub fn find_first(&self, k: &str)-> Option<&str> { for line in self.text.lines() { if line.contains(k) { return Some(line); } } None } pub fn replace_lines(&mut self, k: &str, v:&str) { let mut new_text = String::new(); for line in self.text.lines() { if line.contains(k) { new_text.push_str(v.to_string().as_str()); }else{ new_text.push_str(line); } new_text.push('\n'); } if !self.text.ends_with("\n") { new_text.pop(); } *self.text = new_text; } 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> { text: &'a mut String,}// 2. Implement the methods for the structimpl MutableTextFinder<'_> { pub fn new(text: &mut String) -> MutableTextFinder { MutableTextFinder { text } } pub fn find_first(&self, query: &str) -> Option<&str> { for line in self.text.lines() { if line.contains(query) { return Some(line); } } None } pub fn replace_lines(&mut self, query: &str, replacement: &str) { let new_text = self .text .lines() .map(|line| { if line.contains(query) { replacement.to_string() } else { line.to_string() } }) .collect::<Vec<_>>() .join("\n"); self.text.clear(); self.text.push_str(&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 MutableTextFinder<'_> { pub fn new(text: &mut String) -> MutableTextFinder { MutableTextFinder { text } } pub fn find_first(&self, query: &str) -> Option<&str> { for line in self.text.lines() { if line.contains(query) { return Some(line); } } None } pub fn replace_lines(&mut self, query: &str, replacement: &str) { let new_text = self.text.lines() .map(|line| if line.contains(query) { replacement.to_string() } else { line.to_string() }).collect::<Vec<_>>().join("\n"); self.text.clear(); self.text.push_str(&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> { pub 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(|line| line.contains(key)) } pub fn replace_lines(&mut self, from: &str, to: &str) { let replaced_text = self.text.lines().map(|line| { if line.contains(from) { to.to_owned() } else { line.to_owned() } }).collect::<Vec<String>>().join("\n"); *self.text = replaced_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}
pub struct MutableTextFinder<'a> { text: &'a mut String,}impl MutableTextFinder<'_> { pub fn new(text: &mut String) -> MutableTextFinder { MutableTextFinder { text } } pub fn find_first(&self, query: &str) -> Option<&str> { for line in self.text.lines() { if line.contains(query) { return Some(line); } } None } pub fn replace_lines(&mut self, query: &str, replacement: &str) { let new_text = self.text.lines() .map(|line| if line.contains(query) { replacement.to_string() } else { line.to_string() }).collect::<Vec<_>>().join("\n"); self.text.clear(); self.text.push_str(&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("Learning Rust", "Replace 2"); println!("{}", finder.get_text()); // Should print the modified text finder.replace_lines("Fun with Rustaceans", "Replace 3"); println!("{}", finder.get_text()); // Should print the modified text 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, word: &str) -> Option<&str>{ self.text.lines().find(|l| l.contains(word)) } pub fn get_text(&self) -> &str{ self.text } pub fn replace_lines(&mut self, word1: &str, word2: &str){ let lines = self.text.lines() .map(|line| { if line.contains(word1) { word2.to_string() } else { line.to_string() } }).collect::<Vec<String>>(); *self.text = 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(text: &'a mut String) -> Self { 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, old_str: &str, new_str: &str) { let new_text = self .text .lines() .map(|line| match line.contains(old_str) { true => new_str, false => line, }) .collect::<Vec<&str>>() .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> { my_string: &'a mut String}// 2. Implement the methods for the structimpl<'a> MutableTextFinder<'a> { pub fn new(a: &'a mut String) -> Self { MutableTextFinder {my_string: a} } pub fn find_first(&self, search: &str) -> Option<&str> { self.my_string.lines().find(|x| x.contains(search)) } pub fn replace_lines(&mut self, old_str: &str, new_str: &str) { let new_text = self.my_string.lines().map(|x| { match x.contains(old_str) { true => new_str, false => x } }).collect::<Vec<&str>>().join("\n");; *self.my_string = new_text; } pub fn get_text(&self) -> &String { &self.my_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> { text: &'a mut String,}impl<'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, replacement: &str) { // let mut modified_text = String::new(); // for line in self.text.lines() { // if line.contains(keyword) { // modified_text.push_str(replacement); // } else { // modified_text.push_str(line); // } // modified_text.push_str("\n"); // } // *self.text = modified_text.trim_end().to_string(); // } pub fn replace_lines(&mut self, keyword: &str, replacement: &str) { let new_text: String = self .text .lines() .map(|line| { if line.contains(keyword) { replacement } else { line } }) .collect::<Vec<&str>>() .join("\n"); *self.text = new_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,}impl<'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, replacement: &str) { // let mut modified_text = String::new(); // for line in self.text.lines() { // if line.contains(keyword) { // modified_text.push_str(replacement); // } else { // modified_text.push_str(line); // } // modified_text.push_str("\n"); // } // *self.text = modified_text.trim_end().to_string(); // } pub fn replace_lines(&mut self, keyword: &str, replacement: &str) { let new_text: String = self .text .lines() .map(|line| { if line.contains(keyword) { replacement } else { line } }) .collect::<Vec<&str>>() .join("\n"); *self.text = new_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,}impl <'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, replacement: &str) { let mut modified_text = String::new(); for line in self.text.lines() { if line.contains(keyword) { modified_text.push_str(replacement); } else { modified_text.push_str(line); } modified_text.push_str("\n"); } *self.text = modified_text.trim_end().to_string(); } // pub fn replace_lines(&mut self, keyword: &str, replacement: &str) { // let new_text: Vec<String> = self // .text // .lines() // .map(|line| { // if line.contains(keyword) { // replacement.to_string() // } else { // line.to_string() // } // }) // .collect(); // self.text = new_text.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(text: &'a mut String) -> Self { MutableTextFinder { text, } } pub fn find_first(&self, val: &str) -> Option<&str> { self.text.lines().find(| x | x.contains(val)) } pub fn replace_lines(&mut self, val1: &str, val2: &str) { *self.text = self.text. lines(). map(|x| if x.contains(val1) {val2} else {x} ). fold(String::with_capacity(self.text.len()), |mut acc, line| { if !acc.is_empty() { acc.push('\n'); } acc.push_str(line); acc }); } pub fn get_text(&self) -> String { self.text.clone() }}// 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 new_text: String = self .text .lines() .map(|line| { if line.contains(keyword) { replacement } else { line } }) .collect::<Vec<&str>>() .join("\n"); *self.text = new_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> { pub haystack: &'a mut String,}impl<'a> MutableTextFinder<'a> { pub fn new(haystack: &'a mut String) -> Self { Self { haystack } } pub fn find_first(&self, key: &str) -> Option<&str> { self.haystack.lines().find(|&l| l.contains(key)) } pub fn replace_lines(&mut self, key: &str, replace: &str) { *self.haystack = self .haystack .lines() .map(|l| if l.contains(key) { replace } else { l }) .collect::<Vec<_>>() .join("\n"); } pub fn get_text(&self) -> &str { &self.haystack }}// 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, keyword: &str) -> Option<&str> { self.text.lines().find(|&line| line.contains(keyword)) } pub fn replace_lines(&mut self, keyword: &str, replacement: &str) { let new_text = self.text.lines().map(|line| if line.contains(keyword) { replacement } else { line }) .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> { pub contents: &'a mut String,}// 2. Implement the methods for the structimpl<'a> MutableTextFinder<'a> { pub fn new(txt: &mut String) -> MutableTextFinder { MutableTextFinder { contents: txt } } pub fn find_first(&self, needle: &str) -> Option<&str> { self.contents.lines().find(|&line| line.contains(needle)) } pub fn get_text(&self) -> &str { &self.contents } pub fn replace_lines(&mut self, old: &str, replace: &str) { *self.contents = self .contents .lines() .map(|line| if line.contains(old) { replace } 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>(&'a mut String);// 2. Implement the methods for the structimpl<'a> MutableTextFinder<'a> { pub fn new(content: &'a mut String) -> Self { Self(content) } 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.lines().map(|line| line.contains(keyword).then_some(replacement).unwrap_or(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> { 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, text: &str) -> Option<&str> { for line in self.text.lines() { if line.contains(text) { return Some(line) } } None } pub fn replace_lines(&mut self, text: &str, repl: &str) { *self.text = self.text.lines().map(|line| { if line.contains(text) { repl } 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> { pub text: &'a mut String}impl<'a> MutableTextFinder<'a> { pub fn new(text: &'a mut String) -> Self { MutableTextFinder { text } } pub fn get_text(&self) -> &str { self.text } pub fn find_first(&self, t: &str) -> Option<&str> { self.text.split('\n').find(|tt| tt.contains(t)) } pub fn find_many(&self, t: &str) -> Vec<&str> { self.text.split('\n').filter(|tt| tt.contains(t)).collect() } pub fn replace_lines(&mut self, from: &str, to: &str) { *self.text = self.text.lines().map(|x| { if x.contains(from) { to } else { x } }).collect::<Vec<_>>().join("\n"); } }// 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(text: &'a mut String) -> Self { MutableTextFinder { text } } pub fn find_first<'b>(&self, keyword: &'b str) -> Option<&str> { self.text.lines() .find(|line| line.contains(keyword)) } pub fn get_text(&self) -> &str { self.text } pub fn replace_lines(&mut self, old: &str, new: &str) { *self.text = self.text.lines() .map(|line| { if line.contains(old) { new } 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> { content: &'a mut String,}// 2. Implement the methods for the structimpl<'a> MutableTextFinder<'a> { pub fn new(content: &'a mut String) -> Self { Self { content } } pub fn get_text(&self) -> &str { self.content } pub fn replace_lines(&mut self, find_str: &str, replace_str: &str) { *self.content = self .content .lines() .map(|x| if x.contains(find_str) { replace_str } else { x }) .collect::<Vec<_>>() .join("\n"); } pub fn find_first(&self, keyword: &str) -> Option<&str> { self.content.lines().find(|x| x.contains(keyword)) }}// 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, text: &str) -> Option<&str> { self.text.lines().find(|line| line.contains(text)) } 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) -> &String { self.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, key: &str) -> Option<&str> { self.text.lines().find(|line| line.contains(key)) } pub fn replace_lines(&mut self, key: &str, replacement: &str) { *self.text = self .text .lines() .map(|line| { if line.contains(key) { 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}
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) { return replacement; } else { return 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 { MutableTextFinder { text } } pub fn find_first(&self, t: &'a str) -> Option<&str> { self.text.lines().find(|x| x.contains(t)) } pub fn replace_lines(&mut self, t1: &str, t2: &str) { *self.text = self .text .lines() .map(|x| { if x.contains(t1) { t2.to_string() } else { x.to_string() } }) .collect::<Vec<String>>() .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}