In Rust, structs can store references in addition to owned values. This allows you to create powerful and memory-efficient abstractions by reusing data rather than duplicating it. However, using references in structs requires careful attention to lifetimes to ensure that the references remain valid.
In this challenge, you will create a struct named TextFinder that holds a reference to a string slice (&str). This struct will be used to perform search operations on the string slice by finding lines that contain a given keyword.
TextFinder that holds a reference to a string slice.new() that takes a string slice and returns a TextFinder instance.find_first that returns the first line containing the keyword, or None if no match is found.find_many that returns a vector of all lines containing the keyword.If you're stuck, you may find the following hints useful:
pub struct TextFinder<'a> {
text: &'a str,
}.lines() on a &str to split it into lines and .contains() to check if a string contains a substring..find() or .filter() can simplify search operations.// 1. Define the structpub struct TextFinder<'a> { text: &'a str}// 2. Implement the struct and define the methodsimpl<'a> TextFinder<'a> { pub fn new(text: &'a str) -> Self { Self {text} } pub fn find_first(&self, text: &str) -> Option<&str> { self.text.lines().find(|&line| line.contains(text)) } pub fn find_many(&self, text: &str) -> Vec<&str> { self.text.lines().filter(|&line| line.contains(text)).collect() }}// Example usagepub 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. Define the structpub struct TextFinder<'a>(Vec<&'a str>);// 2. Implement the struct and define the methodsimpl<'a> TextFinder<'a> { pub fn new(text: &'a str) -> Self { Self(text.split('\n').collect()) } pub fn find_first(&self, text_to_find: &str) -> Option<&str> { self.0 .iter() .find(|line| line.contains(text_to_find)) .copied() } pub fn find_many(&self, text_to_find: &str) -> Vec<&str> { self.0 .iter() .filter(|line| line.contains(text_to_find)) .copied() .collect() }}// 1. Define the structpub struct TextFinder<'a> { slice: &'a str,}// 2. Implement the struct and define the methodsimpl<'a> TextFinder<'a> { pub fn new(i: &'a str) -> Self { Self { slice: i } } pub fn find_first(&self, i: &str) -> Option<&'a str> { if let Some(index) = self.slice.find(i) { self.slice .lines() .filter(|v| v.contains(i)) .next() } else { None } } pub fn find_many(&self, i: &str) -> Vec<&str> { self.slice .lines() .filter(|v| v.contains(i)) .collect() }}// Example usagepub 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."]}pub struct TextFinder<'a> { text: &'a str,}impl<'a> TextFinder<'a> { pub fn new(text: &'a str) -> Self { TextFinder { text: text } } pub fn find_first(&self, pattern: &str) -> Option<&str> { self.text.lines().find(|line| line.contains(pattern)) } pub fn find_many(&self, pattern: &str) -> Vec<&str> { self.text .lines() .filter(|line| line.contains(pattern)) .collect() }}// 1. Define the structpub struct TextFinder<'a> { text: &'a str,}impl TextFinder<'_> { pub fn new(text: &str) -> TextFinder<'_> { TextFinder { text } } pub fn find_first(&self, slice: &str) -> Option<&str> { self.text.lines().find(move |line| line.contains(slice)) } pub fn find_many(&self, slice: &str) -> Vec<String> { self.text .lines() .filter(|line| line.contains(slice)) .map(|line| line.to_string()) .collect() }}// 2. Implement the struct and define the methods// Example usagepub 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. Define the structpub struct TextFinder { pub s: Vec<String>}impl TextFinder { pub fn new(x: &str) -> TextFinder { let p = x.lines().map(|item| { item.to_string() }).collect(); TextFinder { s: p } } pub fn find_first(&self, f: &str)-> Option<&str> { for item in &self.s { if item.contains(f) { return Some(item) } } None } pub fn find_many(&self, f: &str) -> Vec<&str> { let mut v = Vec::new(); for item in &self.s { if item.contains(f) { v.push(item.as_str()); } } v }}// 2. Implement the struct and define the methods// Example usagepub 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."]}pub struct TextFinder<'a> { lines: Vec<&'a str>,}// 2. Implement the struct and define the methodsimpl<'a> TextFinder<'a> { pub fn new(text: &'a str) -> Self { Self { lines: text.split('\n').collect(), } } pub fn find_first(&self, pattern: &str) -> Option<&str> { self.lines .iter() .find(|line| line.contains(pattern)) .map(|v| *v) } pub fn find_many(&self, pattern: &str) -> Vec<&str> { self.lines .iter() .filter(|line| line.contains(pattern)) .map(|v| *v) .collect() }}// Example usagepub 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. Define the structpub struct TextFinder<'a> { text: &'a str,}// 2. Implement the struct and define the methodsimpl<'a> TextFinder<'a> { pub fn new(text: &'a str) -> Self { TextFinder { text } } pub fn find_first(&self, query: &str) -> Option<&str> { self.text.lines().find(|line| line.contains(query)) } pub fn find_many(&self, query: &str) -> Vec<&str> { self.text .lines() .filter(|line| line.contains(query)) .collect() }}// Example usagepub 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. Define the structpub struct TextFinder<'a> { pub text: &'a str,}impl<'a> TextFinder<'a> { pub fn new(text: &'a str) -> Self { Self { text } } pub fn find_first(&self, w: &str) -> Option<&str> { match self.find_many(w) { v if !v.is_empty() => Some(v[0]), _ => None, } } pub fn find_many(&self, w: &str) -> Vec<&str> { self.text.lines().filter(|x| x.contains(w)).collect() }}// 2. Implement the struct and define the methods// Example usagepub 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. Define the structpub struct TextFinder <'a>{ text: &'a str,}// 2. Implement the struct and define the methodsimpl<'a> TextFinder<'a> { pub fn new(text:&'a str) -> Self { TextFinder{ text: text, } } pub fn find_first(&self,word: &str)->Option<&str>{ for line in self.text.lines() { if line.contains(word){ return Some(line); } } None } pub fn find_many(&self, word: &str) -> Vec<&str> { let mut retvec = Vec::new(); for line in self.text.lines() { if line.contains(word) { retvec.push(line); } } retvec.to_owned() }}// Example usagepub 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. Define the structpub struct TextFinder<'a> { text: &'a str,}// 2. Implement the struct and define the methodsimpl<'a> TextFinder<'a> { pub fn new(text: &'a str) -> Self { Self { text } } pub fn find_first(&self, s: &str) -> Option<&str> { for line in self.text.lines() { if line.contains(s) { return Some(line); } } None } pub fn find_many(&self, s: &str) -> Vec<&str> { self.text.lines().filter(|line| line.contains(s)).collect() }}// Example usagepub 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. Define the structpub struct TextFinder<'a> { pub content: &'a str,}impl<'a> TextFinder<'a> { pub fn new(content: &'a str) -> TextFinder { TextFinder { content: content } } pub fn find_first(&'a self, text: &'a str) -> Option<&'a str> { self.content.split("\n").find(|line| line.contains(text)) } pub fn find_many(&'a self, text: &'a str) -> Vec<&'a str> { self.content.split("\n").filter(|line| line.contains(text)).collect() }}// 2. Implement the struct and define the methods// Example usagepub 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. Define the structpub struct TextFinder<'a> { text: &'a str}// 2. Implement the struct and define the methodsimpl<'a> TextFinder<'a> { pub fn new(input: &'a str) -> Self { Self{ text: input } } pub fn find_first(&self, key: &str) -> Option<&str> { if key.chars().count() == 0 { return self.text.lines().next(); } for line in self.text.lines() { if let Some(lineContains) = line.find(key) { return Some(line); } } None } pub fn find_many(&self, key: &str) -> Vec<&str> { let mut result: Vec<&str> = Vec::new(); for line in self.text.lines() { if let Some(lineContains) = line.find(key) { result.push(line); } } result }}// Example usagepub 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."]}pub struct TextFinder<'a> { pub s: &'a str,}impl<'a> TextFinder<'a> { pub fn new(s: &'a str) -> Self { Self { s } } pub fn find_first(&self, keyword: &'a str) -> Option<&'a str> { self.s.lines().find(|&i| i.contains(keyword)) } pub fn find_many(&self, keyword: &str) -> Vec<&str> { self.s.lines().filter(|i| i.contains(keyword)).collect() }}pub struct TextFinder<'a> { pub s: &'a str,}impl<'a> TextFinder<'a> { pub fn new(s: &'a str) -> Self { Self { s } } pub fn find_first(&self, keyword: &'a str) -> Option<&'a str> { self.s .split('\n') .collect::<Vec<_>>() .iter() .find(|&i| i.contains(keyword)) .map(|v| v as _) } pub fn find_many(&self, keyword: &'a str) -> Vec<&'a str> { let mut res: Vec<&'a str> = vec![]; let v = self.s.split('\n').collect::<Vec<_>>(); for i in v.iter() { if i.contains(keyword) { res.push(i); } } res }}// 1. Define the structpub struct TextFinder<'a> { pub text: &'a str,}// 2. Implement the struct and define the methodsimpl<'a> TextFinder<'a> { pub fn new(text: &'a str) -> Self { Self { text } } pub fn find_first(&self, word: &str) -> Option<&str> { self.text.lines().find(|line| line.contains(word)) } pub fn find_many(&self, word: &str) -> Vec<&str> { self.text.lines().filter(|line| line.contains(word)).collect() }}// Example usagepub 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. Define the structpub struct TextFinder<'a>{ text: &'a str,}// 2. Implement the struct and define the methodsimpl<'a> TextFinder<'a> { pub fn new(text1:&'a str) -> Self { TextFinder { text:text1 } } pub fn find_first(&self,text1:&'a str)->Option<&str>{ let splitted_str = self.text.lines(); for x in splitted_str{ if x.contains(text1) { return Some(x); } } None } pub fn find_many(&self,text1:&'a str)->Vec<&str>{ let splitted_str = self.text.lines(); let mut ans = Vec::new(); for x in splitted_str { if x.contains(text1){ ans.push(x) } } return ans; }}// Example usagepub 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."]}pub struct TextFinder<'a> { text: &'a str,}impl<'a> TextFinder<'a> { pub fn new(text: &'a str) -> Self { Self { text } } pub fn find_first(&self, needle: &str) -> Option<&'a str> { self.text.lines().find(|line| line.contains(needle)) } pub fn find_many(&self, needle: &str) -> Vec<&'a str> { self.text .lines() .filter(|line| line.contains(needle)) .collect() }}pub struct TextFinder<'a> { text: &'a str,}impl<'a> TextFinder<'a> { pub fn new(text: &'a str) -> Self { Self { text } } pub fn find_first(&self, needle: &'a str) -> Option<&'a str> { self.text.lines().find(|line| line.contains(needle)) } pub fn find_many(&self, needle: &'a str) -> Vec<&'a str> { self.text .lines() .filter(|line| line.contains(needle)) .collect() }}// 1. Define the structpub struct TextFinder<'a> { pub text: &'a str,}// 2. Implement the struct and define the methodsimpl<'a> TextFinder<'a> { pub fn new(text: &'a str) -> Self { Self { text } } pub fn find_first(&self, keyword: &str) -> Option<&str> { self.text.lines().filter(|s| s.contains(keyword)).next() } pub fn find_many(&self, keyword: &str) -> Vec<&str> { self.text.lines().filter(|s| s.contains(keyword)).collect() }}// Example usagepub 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. Define the structpub struct TextFinder<'a> { pub reference: &'a str,}// 2. Implement the struct and define the methodsimpl<'a> TextFinder<'a> { pub fn new(slice: &'a str) -> Self { Self { reference: slice } } pub fn find_first(&self, keyword: &str) -> Option<&str> { for line in self.reference.lines() { if line.contains(keyword){ return Some(&line) } } None } pub fn find_many(&self, keyword: &str) -> Vec<&str> { let mut finds: Vec<&str> = Vec::new(); for line in self.reference.lines() { if line.contains(keyword){ finds.push(&line); } } finds }}// Example usagepub 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. Define the structpub struct TextFinder<'a> { text: &'a str,}impl<'a> TextFinder<'a> { pub fn new(text: &'a str) -> Self { TextFinder { text } } pub fn find_first(&self, pat: &str) -> Option<&'a str> { self.text.lines().find(|line| line.contains(pat)) } pub fn find_many(&self, pat: &str) -> Vec<&'a str> { self.text.lines().filter(|line| line.contains(pat)).collect() }}// 2. Implement the struct and define the methods// Example usagepub 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."]}pub struct TextFinder<'a> { pub text: &'a str,}impl<'a> TextFinder<'a> { pub fn new(text: &'a str) -> Self { TextFinder { text } } pub fn find_first(&self, keyword: &str) -> Option<&str> { self.text.lines().find(|line| line.contains(keyword)) } pub fn find_many(&self, keyword: &str) -> Vec<&str> { self.text .lines() .filter(|line| line.contains(keyword)) .collect() }}// Example usagepub 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. Define the structpub struct TextFinder <'a>{ pub haystack : &'a str,}// 2. Implement the struct and define the methodsimpl <'a> TextFinder <'a> { pub fn new (text : &'a str) -> TextFinder<'a> { return TextFinder { haystack: text}; } pub fn find_first(&self,needle : &str) -> Option<&'a str>{ return self.haystack.lines().find(|&x| x.contains(needle)); } pub fn find_many(&self,needle : &str) -> Vec<&'a str>{ return self.haystack.lines().filter(|&x| x.contains(needle)).collect(); }}// Example usagepub 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."]}pub struct TextFinder<'a> { text: &'a str,}impl<'a> TextFinder<'a> { pub fn new(text: &'a str) -> Self { TextFinder { text } } pub fn find_first(&self, keyword: &str) -> Option<&str> { self.text.lines().find(|line| line.contains(keyword)) } pub fn find_many(&self, keyword: &str) -> Vec<&str> { self.text .lines() .filter(|line| line.contains(keyword)) .collect() }}// Example usagepub 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."]}pub struct TextFinder<'a> { text: &'a str,}impl<'a> TextFinder<'a> { pub fn new(text: &'a str) -> Self { TextFinder { text } } pub fn find_first(&self, keyword: &'a str) -> Option<&'a str> { self.text.lines().find(|line| line.contains(keyword)) } pub fn find_many(&self, keyword: &'a str) -> Vec<&'a str> { self.text .lines() .filter(|line| line.contains(keyword)) .collect() }}// Example usagepub 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. Define the structpub struct TextFinder<'a> { pub text: &'a str,}// 2. Implement the struct and define the methodsimpl<'a> TextFinder<'a> { pub fn new(text: &'a str) -> Self { Self { text } } pub fn find_first(&self, query: &str) -> Option<&str> { self.text.lines().find(|line| line.contains(query)) } pub fn find_many(&self, query: &str) -> Vec<&str> { self.text .lines() .filter(|line| line.contains(query)) .collect() }}// 2. Implement the struct and define the methods// Example usagepub 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. Define the structpub struct TextFinder<'a> { pub text: &'a str,}// 2. Implement the struct and define the methodsimpl<'a> TextFinder<'a> { pub fn new(text: &'a str) -> Self { Self { text } } pub fn find_first(&self, query: &str) -> Option<&str> { self.text.lines().find(|line| line.contains(query)) } pub fn find_many(&self, query: &str) -> Vec<&str> { self.text .lines() .filter(|line| line.contains(query)) .collect() }}// 2. Implement the struct and define the methods// Example usagepub 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. Define the structpub struct TextFinder<'a> { pub text: &'a str,}// 2. Implement the struct and define the methodsimpl<'a> TextFinder<'a> { pub fn new(text: &'a str) -> Self { Self { text } } pub fn find_first(&self, query: &str) -> Option<&str> { self.text.lines().find(|line| line.contains(query)) } pub fn find_many(&self, query: &str) -> Vec<&str> { self.text .lines() .filter(|line| line.contains(query)) .collect() }}// 2. Implement the struct and define the methods// Example usagepub 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. Define the structpub struct TextFinder<'a> { pub text: &'a str,}// 2. Implement the struct and define the methodsimpl<'a> TextFinder<'a> { pub fn new(text: &'a str) -> Self { Self { text } } pub fn find_first(&self, query: &str) -> Option<&str> { self.text.lines().find(|line| line.contains(query)) } pub fn find_many(&self, query: &str) -> Vec<&str> { self.text .lines() .filter(|line| line.contains(query)) .collect() }}// Example usagepub 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. Define the structpub struct TextFinder<'a>{ text : &'a str}// 2. Implement the struct and define the methodsimpl<'a> TextFinder<'a>{ pub fn new(slice : &'a str) -> Self{ Self{text : slice} } pub fn find_first(&self,text_string: &'a str) -> Option<&str>{ self.text.lines().find(|line|line.contains(text_string)) } pub fn find_many(&self, text_string: &'a str) -> Vec<&'a str>{ self.text.lines().filter(|line|line.contains(text_string)).collect() }}// Example usagepub 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. Define the structpub struct TextFinder<'a> { text: &'a str,}impl<'a> TextFinder<'a> { pub fn new(text: &'a str) -> Self { TextFinder { text } } pub fn find_first(&self, key: &str) -> Option<&str> { let lines = self.text.split('\n'); for line in lines { if line.contains(key) { return Some(line); } } None } pub fn find_many(&self, key: &str) -> Vec<&str> { let mut res = Vec::new(); let lines = self.text.split('\n'); for line in lines { if line.contains(key) { res.push(line); } } res }}// 2. Implement the struct and define the methods// Example usagepub 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. Define the structpub struct TextFinder<'a> { pub text: &'a str}impl<'a> TextFinder<'a> { pub fn new(text: &'a str) -> Self { TextFinder { text, } } pub fn find_first(&self, keyword: &'a str) -> Option<&str> { if let Some(line) = self.text.lines().find(|line| line.contains(keyword)) { Some(line) } else { None } } pub fn find_many(&self, keyword: &'a str) -> Vec<&str> { let lines = self.text .lines() .filter(|line| line.contains(keyword)) .collect::<Vec<&str>>(); lines }}// 2. Implement the struct and define the methods// Example usagepub 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. Define the structpub struct TextFinder<'a> { text : &'a str}// 2. Implement the struct and define the methodsimpl<'a> TextFinder<'a> { pub fn new(text:&'a str) -> Self { TextFinder {text: text} } pub fn find_first(&self,s: &str) -> Option<&str> { self.text.lines().find(|l| l.contains(s)) } pub fn find_many(&self,s: &str)->Vec<&str> { self.text.lines().filter(|l| l.contains(s)).collect() }}// Example usagepub 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. Define the structpub struct TextFinder<'a> { text : &'a str}// 2. Implement the struct and define the methodsimpl<'a> TextFinder<'a> { pub fn new(text:&'a str) -> Self { TextFinder { text: text } } pub fn find_first(&self,s:&str) -> Option<&str> { self.text.lines().find(|l| l.contains(s)) } pub fn find_many(&self,s:&str)->Vec<&str> { self.text.lines().filter(|l|l.contains(s)).collect() }}// Example usagepub 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."]} pub struct TextFinder<'a> { text: &'a str,}impl<'a> TextFinder<'a> { pub fn new(text: &'a str) -> Self { Self { text } } pub fn find_first(&self, keyword:&str) -> Option<&str> { self.text.lines().find(|l| l.contains(keyword)) } pub fn find_many(&self, keyword:&str) -> Vec<&str> { self.text.lines().filter(|l| l.contains(keyword)).collect() }}// Example usagepub 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."]}pub struct TextFinder<'a> { pub string: &'a str,}impl<'a> TextFinder<'a> { pub fn new(string: &'a str) -> Self { Self { string } } pub fn find_first(&self, keyword: &str) -> Option<&str> { self.string.lines().find(|&l| l.contains(keyword)) } pub fn find_many(&self, keyword: &str) -> Vec<&str> { self.string.lines().filter(|&l| l.contains(keyword)).collect() }}// Example usagepub 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. Define the structpub struct TextFinder<'a> { text: &'a str,}impl<'a> TextFinder<'a> { pub fn new(text: &str) -> TextFinder { TextFinder { text } } pub fn find_first(&self, keyword: &str) -> Option<&str> { self.text.lines().find(|line| line.contains(keyword)) } pub fn find_many(&self, keyword: &str) -> Vec<&str> { self.text .lines() .filter(|line| line.contains(keyword)) .collect() }}// Example usagepub 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. Define the structpub struct TextFinder<'a> { text: &'a str,}// 2. Implement the struct and define the methodsimpl<'a> TextFinder<'a> { pub fn new(t: &'a str) -> Self { Self { text: t } } pub fn find_first(&self, keyword: &str) -> Option<&'a str> { self.text.lines().find(|line| line.contains(keyword)) } pub fn find_many(&self, keyword: &str) -> Vec<&'a str> { self.text.lines().filter(|line| line.contains(keyword)).collect() }}// Example usagepub 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."]}pub struct TextFinder<'a> { text: &'a str,}impl<'a> TextFinder<'a> { pub fn new(text: &'a str) -> Self { Self { text } } pub fn find_first(&self, keyword: &str) -> Option<&'a str> { self.text.lines().find(|line| line.contains(keyword)) } pub fn find_many(&self, keyword: &str) -> Vec<&'a str> { self .text .lines() .filter(|line| line.contains(keyword)) .collect() }}// 2. Implement the struct and define the methods// Example usagepub 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."]}pub struct TextFinder<'a> { pub text: &'a str,}impl<'a> TextFinder<'a> { pub fn new(s: &'a str) -> Self { Self { text: s, } } pub fn find_first(&self, s: &str) -> Option<&str> { self.text.lines().find(|&x| x.contains(s)) } pub fn find_many(&self, s: &str) -> Vec<&str> { self.text.lines().filter(|&x| x.contains(s)).collect() }}// Example usagepub 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."]}pub struct TextFinder<'a> { pub content: &'a str}impl<'a> TextFinder<'a> { pub fn new(content: &'a str) -> Self { Self { content: content } } pub fn find_first(&self, pat: &str) -> Option<&str> { self.content .split('\n') .find(|el| el.contains(pat)) } pub fn find_many(&self, pat: &str) -> Vec<&str> { self.content .split("\n") .filter(|el| el.contains(pat)) .collect() }}// Example usagepub 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. Define the structpub struct TextFinder<'a> { text: &'a str,}impl <'a>TextFinder<'a> { pub fn new(value: &'a str) -> Self{ TextFinder{text: value} } pub fn find_first(&self, value: &str) -> Option<&str> { self.text.lines().find(|line| line.contains(&value) ) } pub fn find_many(&self, value: &str) -> Vec<&str> { self.text.lines().filter(|line| line.contains(&value) ).collect() }}// 2. Implement the struct and define the methods// Example usagepub 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. Define the structpub struct TextFinder <'a>{ text:&'a str,}// 2. Implement the struct and define the methodsimpl <'a> TextFinder <'a> { pub fn new (value:&'a str) -> Self { TextFinder { text: value, } } pub fn find_first (& 'a self , key:& 'a str)-> Option <&'a str> { self.text.lines().find(|line| line.contains(&key) ) } pub fn find_many (& 'a self , key:& 'a str) -> Vec <& 'a str> { self.text.lines().filter(|line| line.contains(&key)).collect() } }// Example usagepub 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. Define the structpub struct TextFinder<'a>{ text : &'a str, }impl<'a> TextFinder<'a>{ pub fn new(t :&'a str) -> Self{ Self{text: t} } pub fn find_first(&self, f : &str) -> Option<&'a str>{ for line in self.text.lines(){ if line.contains(f){ return Some(line) } } None } pub fn find_many(&self, f : &str) -> Vec::<&'a str>{ let mut res_vec = vec![]; for line in self.text.lines(){ if line.contains(f){ res_vec.push(line) ; } } res_vec }}// 2. Implement the struct and define the methods// Example usagepub 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. Define the structpub struct TextFinder<'a> { text : &'a str,}impl<'a> TextFinder<'a> { pub fn new(s: &'a str) -> Self { TextFinder{text: s} } pub fn find_first(&self, key: &str) -> Option<&'a str> { self.text.lines() .find(|line| line.contains(&key)) } pub fn find_many(&self, key: &str) -> Vec<&'a str> { let v= self.text.lines() .filter(|line| line.contains(key)) .collect(); v }}// 2. Implement the struct and define the methods// Example usagepub 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. Define the structpub struct TextFinder<'a>(&'a str);impl<'a> TextFinder<'a> { pub fn new(text: &'a str) -> Self { Self(text) } pub fn find_first(&self, keyword: &str) -> Option<&str> { self.0.lines().find(|line| line.contains(keyword)) } pub fn find_many(&self, keyword: &str) -> Vec<&str> { self.0 .lines() .filter(|line| line.contains(keyword)) .collect() }}// 2. Implement the struct and define the methods// Example usagepub 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. Define the structpub struct TextFinder<'a> { text: &'a str}// 2. Implement the struct and define the methodsimpl<'a> TextFinder<'a> { pub fn new(s: &'a str) -> Self { TextFinder{text: s} } pub fn find_first(&self, search: &str) -> Option<&str> { self.text.lines().find(|s| s.contains(search)) } pub fn find_many(&self, search: &str) -> Vec<&str> { self.text.lines().filter(|s| s.contains(search)).collect::<Vec<&str>>() }}// Example usagepub 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. Define the structpub struct TextFinder<'a> { pub text: &'a str,}// 2. Implement the struct and define the methodsimpl<'a> TextFinder<'a> { pub fn new(s: &'a str) -> Self { Self { text: s, } } pub fn find_first(&self, key : &str) -> Option<&'a str> { for line in self.text.lines() { if line.contains(key) { return Some(line); } } // key not found in any lines return None } // TODO: COULD USE ITERATORS .find() or .filter() with closures instead here... /* pub fn find_first(&self, keyword: &str) -> Option<&'a str> { self.text.lines().find(|line| line.contains(keyword)) } */ pub fn find_many(&self, key : &str) -> Vec<&'a str> { let mut output = vec![]; for line in self.text.lines() { if line.contains(key) { output.push(line); } } output } // TODO: COULD USE ITERATORS .find() or .filter() with closures instead here... /* pub fn find_many(&self, keyword: &str) -> Vec<&'a str> { self.text.lines().filter(|line| line.contains(keyword)).collect() } */}// Example usagepub 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. Define the structpub struct TextFinder <'a> { txt: &'a str,}// 2. Implement the struct and define the methodsimpl<'a> TextFinder<'a> { pub fn new(txt: &'a str) -> Self { Self {txt} } pub fn find_first(&self, txt: &'a str) -> Option<&'a str> { self.txt.lines().find(|l| l.contains(txt)) } pub fn find_many(&self, txt: &'a str) -> Vec<&'a str> { self.txt.lines().filter(|l|l.contains(txt)).collect() }}// Example usagepub 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."]}