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> { pub content: &'a str, }// 2. Implement the struct and define the methodsimpl<'a> TextFinder<'a> { pub fn new(t: &'a str) -> Self { Self { content: t } } pub fn find_first(&self, target: &'a str) -> Option<&'a str> { for l in self.content.lines() { if l.contains(target) { return Some(l); } } return None; } pub fn find_many(&self, target: &'a str) -> Vec<&'a str> { let mut res = vec![]; for l in self.content.lines() { if l.contains(target) { res.push(l); } } return res; }}// 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> { s: &'a str,}// 2. Implement the struct and define the methodsimpl<'a> TextFinder<'a> { pub fn new(s: &'a str) -> Self { Self { s: s, } } pub fn find_first(&self, keyword: &str) -> Option<&str> { for l in self.s.split("\n") { if l.contains(keyword) { return Some(l); } } return None; } pub fn find_many(&self, keyword: &str) -> Vec<&str> { let mut output = vec![]; for l in self.s.split("\n") { if l.contains(keyword) { output.push(l); } } return output; }}// 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> { haystack: &'a str}impl<'a> TextFinder<'a> { pub fn new(text: &'a str) -> TextFinder { Self { haystack: text } } pub fn find_first(&self, needle: &str) -> Option<&str> { self.haystack.lines().find(|l| l.contains(needle)) } pub fn find_many(&self, needle: &str) -> Vec<&str> { self.haystack.lines().filter(|l| l.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."]}
// 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().filter(|s| s.contains(&word)).next()} pub fn find_many(&self, word: &str) -> Vec<&'a str> { self.text.lines() .filter(|&w| w.to_lowercase() .contains(&word.to_lowercase())).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> { s1: &'a str}impl<'a> TextFinder<'a> { pub fn new(s1: &'a str) -> Self { Self { s1 } } pub fn find_first(&self, s: &str) -> Option<&'a str> { self.s1.lines().filter(|sub| sub.contains(s)).collect::<Vec<_>>().get(0).copied() } pub fn find_many(&self, s: &str) -> Vec<&'a str> { self.s1.lines().filter(|sub| sub.contains(s)).collect::<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> { s1: &'a str}impl<'a> TextFinder<'a> { pub fn new(s1: &'a str) -> Self { Self { s1 } } pub fn find_first(&self, s: &str) -> Option<&'a str> { self.s1.lines().filter(|sub| sub.contains(s)).collect::<Vec<_>>().get(0).copied() } pub fn find_many(&self, s: &str) -> Vec<&'a str> { self.s1.lines().filter(|sub| sub.contains(s)).collect::<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,}// 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, 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>{ text: &'a str}impl<'a> TextFinder<'a>{ pub fn new(text: &'a str)-> Self{ TextFinder{text} } pub fn find_first(&self, text: &str)-> Option<&str>{ self.text.lines().find(|t| t.contains(text)) } pub fn find_many(&self, text: &str)->Vec<&str>{ self.text.lines().filter(|t| t.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(text: &'a str) -> Self { TextFinder { 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> { slice: &'a str,}// 2. Implement the struct and define the methodsimpl <'a> TextFinder<'a> { pub fn new(slice: &'a str) -> Self { Self { slice } } pub fn find_first(&self, pattern: &str) -> Option<&str> { self.slice.split('\n').find(|line| line.contains(pattern)) } pub fn find_many(&self, pattern: &str) -> Vec<&str> { self.slice.split('\n').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<&'a str> { for l in self.text.lines() { if l.contains(pattern) { return Some(l); } } None } pub fn find_many(&self, pattern: &str) -> Vec<&'a str> { self.text.lines().filter(|l| l.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) -> TextFinder<'a> { TextFinder { 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, pat: &str) -> Option<&'a str> { let mut lines = self.text.lines(); 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() }}// 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, pat: &str) -> Option<&'a str> { let mut lines = self.text.lines(); lines.find(|&line| line.contains(pat)) } pub fn find_many(&self, pat: &str) -> Vec<&'a str> { let mut result = Vec::new(); for line in self.text.lines() { if line.contains(pat) { 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 text: &'a str,}impl<'a> TextFinder<'a> { pub fn new(s: &'a str) -> Self { TextFinder { text: s } } pub fn find_first(&self, keyword: &str) -> Option<&str> { let binding = self.find_many(keyword); let o = binding.first(); match o { Some(s) => { return Some(*s); } None => None } } pub fn find_many(&self, keyword: &str) -> Vec<&str> { return self.text.lines().filter(|s| s.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> { s: &'a str,}// 2. Implement the struct and define the methodsimpl<'a> TextFinder<'a> { pub fn new(s: &'a str) -> Self { Self { s } } pub fn find_first(&self, keyword: &str) -> Option<&str> { match self.s.lines().find(|line| line.contains(keyword)) { Some(x) => Some(x), None => None, } } pub fn find_many(&self, keyword: &str) -> Vec<&str> { let mut sentences = vec![]; for sen in self.s.lines() { if sen.contains(keyword) { sentences.push(sen); } } sentences }}// 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: &'a str) -> Option<&'a str>{ self.text.split("\n").find(|&x| x.contains(key)) } pub fn find_many(&self, key: &'a str) -> Vec<&'a str>{ self.text.split("\n").filter(|&x| x.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."]}
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, slice: &str) -> Option<&str> { self.text.lines().find(|line| line.contains(slice)) } pub fn find_many(&self, slice: &str) -> Vec<&str> { self.text.lines().filter(|line| line.contains(slice)).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(|x| x.contains(keyword)) } pub fn find_many(&self, keyword: &str) -> Vec<&str> { self.text.lines().filter(|x| x.contains(keyword)).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> { s: &'a str}// 2. Implement the struct and define the methodsimpl<'a> TextFinder<'a> { pub fn new(s: &'a str) -> Self { Self { s } } pub fn find_first(&self, keyword: &'a str) -> Option<&'a str> { //println!("S: {}, Keyword: {}", self.s, keyword); for line in self.s.split('\n') { if line.contains(&keyword) { return Some(line); } } None } pub fn find_many(&self, keyword: &'a str) -> Vec<&'a str> { let mut v = Vec::new(); for line in self.s.split('\n') { if line.contains(&keyword) { v.push(line); } } v }}// 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: &str) -> TextFinder { TextFinder{ text: text } } pub fn find_first(&self, string: &str) -> Option<&str> { self.text .lines() .find(|l| l.contains(string)) } pub fn find_many(&self, string: &str) -> Vec<&str> { self.text .lines() .filter(|l| l.contains(string)) .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 inner: &'a str,}// 2. Implement the struct and define the methodsimpl<'a> TextFinder<'a> { pub fn new(text: &'a str) -> Self { Self { inner: text } } pub fn find_first(&self, search: &str) -> Option<&str> { self.inner.lines().find(|it| it.contains(search)) } pub fn find_many(&self, search: &str) -> Vec<&str> { self.inner .lines() .filter(|it| it.contains(search)) .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 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, 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."]}
// 1. Define the struct// 1. Define the structpub struct TextFinder<'a>(&'a str);impl<'a> TextFinder<'a> { pub fn new(text: &'a str) -> Self { TextFinder(text) } pub fn find_first(&self, text: &str) -> Option<&'a str> { let lines = self.0.split('\n').collect::<Vec<&str>>(); lines.iter().find(|line| line.contains(text)).map(|line| *line) } pub fn find_many(&self, text: &str) -> Vec<&'a str> { let lines = self.0.split('\n').collect::<Vec<&str>>(); lines.iter().filter(|line| line.contains(text)).map(|line| *line).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."]}