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> { slice : &'a str}// 2. Implement the struct and define the methodsimpl<'a> TextFinder <'a>{ pub fn new(slice: &'a str) -> Self { Self { slice: slice } } pub fn find_first(&self, value: &str) -> Option<&'a str> { self.slice.split("\n").find(|x| x.contains(value)) } pub fn find_many(&self, value: &str) -> Vec<&'a str> { self.slice.split("\n").filter(|x| x.contains(value)).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, q: &str) -> Option<&'a str> { return self.text.split("\n").find(|l| l.contains(q)); } pub fn find_many(&self, q: &str) -> Vec<&'a str> { return self.text.split("\n").filter(|l| l.contains(q)).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 { Self { text } } pub fn find_first(&self, key: &'a str) -> Option<&'a str> { for line in self.text.split('\n') { if line.contains(key) { return Some(line); } } None } pub fn find_many(&self, key: &'a str) -> Vec<&'a str> { self.text.split('\n').filter(|s| s.contains(key)).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 } } pub fn find_first(&self, first: &str) -> Option<&str> { self.text.lines().find(|line| line.contains(first)) } pub fn find_many(&self, pattern: &str) -> Vec<&str> { self.text .lines() .filter(|line| line.contains(pattern)) .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> { haystack: &'a str,}impl TextFinder<'_> { pub fn new<'a>(haystack: &'a str) -> TextFinder<'a> { TextFinder { haystack } } pub fn find_first(&self, needle: &str) -> Option<&str> { for line in self.haystack.lines() { if line.contains(needle) { return Some(line); } } None } pub fn find_many(&self, needle: &str) -> Vec<&str> { let mut result = Vec::new(); for line in self.haystack.lines() { if line.contains(needle) { result.push(line) } } result }}// 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 content: String,}// 2. Implement the struct and define the methodsimpl TextFinder { pub fn new(content: &str) -> Self { Self { content: content.to_string(), } } pub fn find_first(&self, search: &str) -> Option<&str> { self.content.lines().filter(|s| s.contains(search)).next() } pub fn find_many(&self, search: &str) -> Vec<&str> { self.content .lines() .filter(|s| s.contains(search)) .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 { content: String,}// 2. Implement the struct and define the methodsimpl TextFinder { pub fn new(content: &str) -> Self { Self { content: content.to_string(), } } pub fn find_first(&self, search: &str) -> Option<&str> { self.content .split("\n") .into_iter() .filter(|s| s.contains(search)) .next() } pub fn find_many(&self, search: &str) -> Vec<&str> { self.content .split("\n") .filter(|s| s.contains(search)) .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(t: &'a str) -> Self { Self{text: t} } pub fn find_first(&self, keyword: &str) -> Option<&str> { for l in self.text.lines() { if l.contains(keyword) { return Some(l) } } None } pub fn find_many(&self, keyword: &str) -> Vec<&str> { let mut vv = Vec::new(); for l in self.text.lines() { if l.contains(keyword) { vv.push(l); } } vv }}// 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>(&'a str);impl<'a> TextFinder<'a> { pub fn new(text: &'a str) -> Self { TextFinder(text) } pub fn find_first(&self, string: &str) -> Option<&str> { self.0.lines().find(|line| line.contains(string)) } pub fn find_many(&self, string: &str) -> Vec<&str> { self.0.lines().filter(|line| line.contains(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> { pub text: &'a str}impl <'a>TextFinder<'a> { pub fn new(text: &'a str) -> Self { Self { text } } pub fn find_first(&self, search: &str) -> Option<&str> { let lines = self.text.split('\n'); for line in lines { if line.contains(search) { return Some(line); } } return None } pub fn find_many(&self, search: &str) -> Vec<&str> { let lines = self.text.split('\n'); lines.filter(|line| line.contains(search)).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}impl <'a>TextFinder<'a> { pub fn new(text: &'a str) -> Self { Self { text } } pub fn find_first(&self, search: &str) -> Option<&str> { let lines = self.text.split('\n'); for line in lines { if line.contains(search) { return Some(line); } } return None } pub fn find_many(&self, search: &str) -> Vec<&str> { let lines = self.text.split('\n'); lines.filter(|line| line.contains(search)).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."]}
use std::cmp::PartialEq;// 1. Define the struct#[derive(PartialEq)]pub 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 { TextFinder { 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 find_many(&self, input: &str) -> Vec<&str> { self.text .lines() .filter(|line| line.contains(input)) .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 { TextFinder { text: text, } } pub fn find_first(&self, keyword: &str) -> Option<&str> { for line in self.text.lines() { if line.contains(keyword) { return Some(line) } } None } pub fn find_many(&self, keyword: &str) -> Vec<&str> { let mut matches: Vec<&str> = Vec::new(); for line in self.text.lines() { if line.contains(keyword) { matches.push(line); } } matches }}// 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 { TextFinder { text: text, } } pub fn find_first(&self, keyword: &str) -> Option<&str> { for line in self.text.lines() { if line.contains(keyword) { return Some(line) } } None } pub fn find_many(&self, keyword: &str) -> Vec<&str> { let mut matches: Vec<&str> = Vec::new(); for line in self.text.lines() { if line.contains(keyword) { matches.push(line); } } matches }}// 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<'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 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) -> TextFinder { TextFinder { text } } pub fn find_first(&'a self, needle: &str) -> Option<&'a str> { self.text.lines().filter(|l| l.find(needle).is_some()).next() } pub fn find_many(&'a self, needle: &str) -> Vec<&'a str> { self.text.lines().filter(|l| l.find(needle).is_some()).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> { slice: &'a str,}// 2. Implement the struct and fdfdefdfdefine the methodsimpl<'a> TextFinder<'a> { pub fn new(slice: &'a str) -> Self { return TextFinder{slice:slice} } pub fn find_first(&self, term: & str) -> Option<&str> { let lines = self.slice.lines(); for line in lines { if line.contains(&term) { return Some(line); } } return None; } pub fn find_many(&self, term: & str) -> Vec<&str> { let lines = self.slice.lines(); let mut res:Vec<&str> = Vec::with_capacity(lines.clone().count()); for line in lines { if line.contains(&term) { res.push(line); } } return res; // return self.slice.lines().filter(|line| return line.contains(line)); }}// 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> { slice: &'a str,}// 2. Implement the struct and define the methodsimpl<'a> TextFinder<'a> { pub fn new(slice: &'a str) -> Self { return TextFinder{slice:slice} } pub fn find_first(&self, term: & str) -> Option<&str> { let lines = self.slice.lines(); for line in lines { if line.contains(term) { return Some(line); } } return None; } pub fn find_many(&self, term: & str) -> Vec<&str> { let lines = self.slice.lines(); let mut res:Vec<&str> = Vec::with_capacity(lines.clone().count()); for line in lines { if line.contains(term) { res.push(line); } } return res; // return self.slice.lines().filter(|line| return line.contains(line)); }}// 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> { slice: &'a str,}// 2. Implement the struct and define the methodsimpl<'a> TextFinder<'a> { pub fn new(slice: &'a str) -> Self { return TextFinder{slice:slice} } pub fn find_first(&self, term: & str) -> Option<&str> { let lines = self.slice.lines(); for line in lines { if line.contains(term) { return Some(line); } } return None; } pub fn find_many(&self, term: & str) -> Vec<&str> { let lines = self.slice.lines(); let mut res:Vec<&str> = Vec::with_capacity(lines.count()); for line in self.slice.lines() { if line.contains(term) { res.push(line); } } return res; // return self.slice.lines().filter(|line| return line.contains(line)); }}// 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> { slice: &'a str,}// 2. Implement the struct and define the methodsimpl<'a> TextFinder<'a> { pub fn new(slice: &'a str) -> Self { return TextFinder{slice:slice} } pub fn find_first(&self, term: & str) -> Option<&str> { let lines = self.slice.lines(); for line in lines { if line.contains(term) { return Some(line); } } return None; } pub fn find_many(&self, term: & str) -> Vec<&str> { let lines = self.slice.lines(); let mut res = Vec::with_capacity(lines.count()); for line in self.slice.lines() { if line.contains(term) { res.push(line); } } return res; // return self.slice.lines().filter(|line| return line.contains(line)); }}// 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> { slice: &'a str,}// 2. Implement the struct and define the methodsimpl<'a> TextFinder<'a> { pub fn new(slice: &'a str) -> Self { return TextFinder{slice:slice} } pub fn find_first(&self, term: & str) -> Option<&str> { for line in self.slice.lines() { if line.contains(term) { return Some(line); } } return None; } pub fn find_many(&self, term: & str) -> Vec<&str> { let mut res = vec![]; for line in self.slice.lines() { if line.contains(term) { res.push(line); } } return res; // return self.slice.lines().filter(|line| return line.contains(line)); }}// 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 &'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, pattern: &str) -> Option<&str> { self.0.lines().find(|line| line.contains(pattern)) } pub fn find_many(&self, pattern: &str) -> Vec<&str> { self.0.lines().filter(|line| line.contains(pattern)).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 { Self { 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() }}// 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, kwd: &str) -> Option<&str> { let lines: Vec<&str> = self.text.split('\n').collect(); for line in lines { if line.contains(&kwd) { return Some(line); } } None } pub fn find_many(&self, kwd: &str) -> Vec<&str> { let lines: Vec<&str> = self.text.split('\n').collect(); let mut result: Vec<&str> = Vec::new(); for line in lines.iter() { if line.contains(&kwd) { 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."]}
// 1. Define the structpub struct TextFinder<'a> { pub lines: Vec<&'a str>,}// 2. Implement the struct and define the methodsimpl<'a> TextFinder<'a> { pub fn new(text: &'a str) -> Self { TextFinder { lines: text.split('\n').collect(), } } pub fn find_first(&self, word: &str) -> Option<&str> { self.lines.iter().find(|line| line.contains(word)).map(|v| *v) } pub fn find_many(&self, word: &str) -> Vec<&str> { self.lines .iter() .filter(|line| line.contains(word)) .cloned() .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, text: &'a str) -> Option<&'a str> { self.text.lines().find(|&x| x.contains(text)).into() } pub fn find_many(&self, text: &'a str) -> Vec<&str> { self.text .lines() .filter(|&x| x.contains(text)) .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> { 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: &'a str) -> Option<&'a str> { self.text.lines().find(|&x| x.contains(text)).into() } pub fn find_many(&self, text: &'a str) -> Vec<&str> { self.text .lines() .filter(|&x| x.contains(text)) .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> { text: &'a str,}// 2. Implement the struct and define the methodsimpl<'a> TextFinder<'a> { pub fn new(text: &'a str) -> TextFinder<'a> { TextFinder { text } } pub fn find_first(&self, keyword: &str) -> Option<&str> { self.text.lines().find(|ln| ln.contains(keyword)) } pub fn find_many(&self, keyword: &str) -> Vec<&str> { self.text.lines() .filter(|ln| ln.contains(keyword)) .collect::<Vec<&_>>() }}// 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 slice: &'a str,} // 2. Implement the struct and define the methodsimpl<'a> TextFinder<'a> { pub fn new(sl: &'a str) -> Self { Self { slice: sl, } } pub fn find_first(&self, keyword: &str) -> Option<&str> { let str_vec: Vec<&str> = self.slice.split('\n').collect(); for sentence in str_vec.iter() { if sentence.contains(keyword) { return Some(sentence); } } return None; } pub fn find_many(&self, keyword: &str) -> Vec<&str> { let str_vec: Vec<&str> = self.slice.split("\n").collect(); let mut return_vec: Vec<&str> = Vec::new(); for sentence in str_vec.iter() { if sentence.contains(keyword) { return_vec.push(sentence); } } return return_vec; }}// 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> { // Constructor to create a new TextFinder instance pub fn new(text: &'a str) -> Self { TextFinder { text: 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 find all occurrences of a substring pub fn find_many(&self, substring: &str) -> Vec<&str> { self.text.lines().filter(|line| line.contains(substring)).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> { zaw: &'a str,}impl<'a> TextFinder<'a> { pub fn new(text: &'a str) -> Self { Self { zaw: text, } } pub fn find_first(&self, keyword: &str) -> Option<&str> { for line in self.zaw.lines() { if line.contains(keyword) { return Some(line); } } return None; } pub fn find_many(&self, keyword: &str) -> Vec<&str> { self.zaw.lines().filter(|x| x.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 lines: Vec<&'a str>,}impl<'a> TextFinder<'a> { pub fn new(text: &'a str) -> Self { Self { lines: text.split("\n").collect(), } } pub fn find_first(&self, query: &str) -> Option<&str> { self.lines .iter() .find(|line| line.contains(query)) .map(|v| &**v) } pub fn find_many(&self, query: &str) -> Vec<&str> { self.lines .iter() .filter(|line| line.contains(query)) .cloned() .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 vals: &'a str,}// 2. Implement the struct and define the methodsimpl<'a> TextFinder<'a> { pub fn new(vals: &'a str) -> Self { TextFinder { vals } } pub fn find_first(&self, key: &str) -> Option<&'a str> { for line in self.vals.lines(){ if line.contains(key) { return Some(line) } } None } pub fn find_many(&self, key: &str) -> Vec<&'a str> { let mut result = Vec::new(); for line in self.vals.lines(){ if line.contains(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."]}
// 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(&'a self, key: &'a str) -> Option<&'a str> { for line in self.text.lines() { if line.contains(key) { return Some(line); } } None } pub fn find_many(&'a self, key: &'a str) -> Vec<&'a str> { let mut result = Vec::new(); for line in self.text.lines() { if line.contains(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."]}
// 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(&'a self, key: &'a str) -> Option<&'a str> { for line in self.text.lines() { if line.contains(key) { return Some(line); } } None } pub fn find_many(&'a self, key: &'a str) -> Vec<&'a str> { let mut result = Vec::new(); for line in self.text.lines() { if line.contains(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."]}
// 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(&'a self, key: &'a str) -> Option<&'a str> { for line in self.text.lines() { if line.contains(key) { return Some(line); } } None } pub fn find_many(&'a self, key: &'a str) -> Vec<&'a str> { let mut result = Vec::new(); for line in self.text.lines() { if line.contains(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."]}
// 1. Define the structpub struct TextFinder<'a>{ text: &'a str,}impl<'a> TextFinder<'a> { pub fn new(text: &str) -> TextFinder { TextFinder { text: 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 find_many(&self, query: &str) -> Vec<&str> { let mut matches = Vec::new(); for line in self.text.lines() { if line.contains(query) { matches.push(line); } } matches }}// 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 lines: Vec<&'a str>,}// 2. Implement the struct and define the methodsimpl<'a> TextFinder<'a> { pub fn new(text: &'a str) -> Self { let lines: Vec<&str> = text.split("\n").collect(); Self { lines: Vec::from(lines), } } pub fn find_first(&self, word: &str) -> Option<&str> { // let mut result = Vec::new(); for line in &self.lines { if line.contains(word) { return Some(*line); } } None // result } pub fn find_many(&self, word: &str) -> Vec<&str> { let mut result = Vec::new(); for line in &self.lines { if line.contains(word) { 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."]}
// 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, keyword: &str) -> Option<&str> { self.text.lines().find(|x| x.contains(keyword)) } pub fn find_many(&self, keyword: &str) -> Vec<&str> { self.text.lines().filter(|x| x.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> { my_text: &'a str}// 2. Implement the struct and define the methodsimpl<'a> TextFinder<'a> { pub fn new(a: &'a str) -> Self { TextFinder { my_text: a } } pub fn find_first(&self, search: &'a str) -> Option<&'a str> { self.my_text.lines().find(|x| x.contains(search)) } pub fn find_many(&self, search: &'a str) -> Vec<&'a str> { self.my_text.lines().filter(|x| x.contains(search)).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, keyword: &str) -> Option<&str>{ self.text.lines().find(|&e| e.contains(keyword) ) } pub fn find_many(&self, keyword: &str) -> Vec<&str> { self.text.lines().filter(|&e| e.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,}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(|&e| e.contains(keyword) ) } pub fn find_many(&self, keyword: &str) -> Vec<&str> { self.text.lines().filter(|&e| e.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(text: &'a str) -> Self { Self { text } } pub fn find_first(&self, text: &str) -> Option<&'a str> { for line in self.text.lines() { if line.find(text).is_some() { return Some(line) } } None } pub fn find_many(&self, text: &str) -> Vec<&'a str> { self.text.lines() .filter(|line| line.find(text).is_some()) .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 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() }}// 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, r: &str) -> Option<&str> { self.text.lines().find(|x| x.contains(r)) } pub fn find_many(&self, r: &str) -> Vec<&str> { self.text.lines().filter(|x| x.contains(r)).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 haystack: &'a str,}impl<'a> TextFinder<'a> { pub fn new(haystack: &'a str) -> Self { Self { haystack } } pub fn find_first(&'_ self, key: &str) -> Option<&str> { self.haystack .lines() .into_iter() .find(|l| l.contains(key)) } pub fn find_many(&self, key: &str) -> Vec<&str> { self.haystack .lines() .into_iter() .filter(|l| l.contains(key)) .collect() }}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. Define the structpub struct TextFinder<'a> { pub text: &'a str, }impl<'a> TextFinder<'a> { pub fn new(text: &'a str ) -> Self { TextFinder { text: 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>(&'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<&'a str> { self.0.lines().find(|line| line.contains(keyword)) } pub fn find_many(&self, keyword: &str) -> Vec<&'a str> { self.0.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 { pub text: String,}// 2. Implement the struct and define the methodsimpl TextFinder { pub fn new(txt: &str) -> TextFinder { TextFinder { text: txt.to_string(), } } pub fn find_first(&self, needle: &str) -> Option<&str> { let lines = self.text.split("\n"); for line in lines { if line.contains(needle) { return Some(line); } } None } pub fn find_many(&self, needle: &str) -> Vec<&str> { let mut ans: Vec<&str> = vec![]; let lines = self.text.split("\n"); for line in lines { if line.contains(needle) { ans.push(line); } } 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."]}
// 1. Define the structpub struct TextFinder<'a> { pub text: &'a str,}impl<'a> TextFinder<'a> { pub fn new(txt: &'a str) -> TextFinder { TextFinder { text: txt, } } pub fn find_first(&self, needle: &str) -> Option<&str> { let lines = self.text.split("\n"); for line in lines { if line.contains(needle) { return Some(line); } } None } pub fn find_many(&self, needle: &str) -> Vec<&str> { let mut ans: Vec<&str> = vec![]; let lines = self.text.split("\n"); for line in lines { if line.contains(needle) { ans.push(line); } } ans }}// 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."]}