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.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."]}
// 1. Define the structpub struct TextFinder<'a> { value: &'a str,}// 2. Implement the struct and define the methodsimpl<'a> TextFinder<'a> { pub fn new(value: &'a str) -> Self { TextFinder { value: value } } pub fn find_first(&self, find: &str) -> Option<&'a str> { let mut lines = self.value.lines(); lines.find(|line| line.contains(find)) } pub fn find_many(&self, find: &str) -> Vec<&'a str> { let lines = self.value.lines(); lines.filter(|line| line.contains(find)).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(s : &'a str)->Self{ Self{ text: s} } pub fn find_first(&self,keyword : &str)->Option<&str>{ self.text.lines().find(|n| n.contains(keyword)) } pub fn find_many(&self,keyword : &str)->Vec<&str>{ self.text.lines().filter(|n| n.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, word: &str) -> Option<&str> { self.text.lines().find(|t| t.contains(word)) } pub fn find_many(&self, word: &str) -> Vec<&str> { self.text.lines().filter(|t| t.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> { 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, find: &str) -> Option<&str> { self.text.lines().find(|l| l.contains(find)) } pub fn find_many(&self, find: &str) -> Vec<&str> { self.text.lines().filter(|s| s.contains(find)).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, 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 { Self { 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() }}
// 1. Define the structpub struct TextFinder<'a>{ base_text: &'a str,}// 2. Implement the struct and define the methodsimpl<'a> TextFinder<'a> { pub fn new(base_text: &'a str) -> Self { TextFinder { base_text } } pub fn find_first(&self, keyword: &str) -> Option<&str> { for line in self.base_text.lines() { if line.contains(keyword) { return Some(line); } } return None; } pub fn find_many(&self, keyword: &str) -> Vec<&str> { self.base_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 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<'b>(&'a self, keyword: &'b str) -> Option<&'a str> { self.text.lines().find(|l| l.contains(keyword)) } pub fn find_many<'b>(&'a self, keyword: &'b str) -> Vec<&'a 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 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, a: &str) -> Option<&'a str> { let mut lines = self.s.lines(); lines.find(|l| l.contains(a)) } pub fn find_many(&self, a: &str) -> Vec<&'a str> { self.s.lines().filter(|l| l.contains(a)).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 trait TF<'a> { fn find_first(&self, word: &str) -> Option<&'a str>; fn find_many(&self, word: &str) -> Vec<&'a str>; // Changed to return Vec}pub struct TextFinder<'a> { pub slices: &'a str,}// 2. Implement the struct and define the methodsimpl<'a> TF<'a> for TextFinder<'a> { fn find_first(&self, word: &str) -> Option<&'a str> { self.slices.lines().find(|l| l.contains(word)) } fn find_many(&self, word: &str) -> Vec<&'a str> { // Changed to return Vec self.slices .lines() .filter(|l| l.contains(word)) .collect() // Collect all lines into a Vec }}impl<'a> TextFinder<'a> { pub fn new(x: &'a str) -> Self { Self { slices: x, } }}// 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, a: &str) -> Option<&'a str> { let mut lines = self.s.lines(); lines.find(|l| l.contains(a)) } pub fn find_many(&self, a: &str) -> Vec<&'a str> { self.s.lines().filter(|l| l.contains(a)).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(s: &'a str) -> Self{ Self{text: s} } pub fn find_first(&self, s: &str) -> Option<&str> { // self.text.lines().contains(s) self.text.lines().find_map( |x| { if x.contains(s){ Some(x) } else {None} } ) } pub fn find_many(&self, s: &str) -> Vec<&str> { self.text.lines().filter(|x| x.contains(s) ).collect::<Vec<&str>>() // .map(|x|x.unwrap()) }}// 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 { Self { text } } pub fn find_first(&self, key: &str) -> Option<&str> { self.text.lines().find(|line| line.contains(key)) } pub fn find_many(&self, key:&str) -> Vec<&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 struct as a tuple, only access as .0// Better: Indicates it's line-orientedpub 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<&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() }}// 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<&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() }}// 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) -> TextFinder<'a>{ TextFinder { text } } pub fn find_first(&self, pattern: &str)-> Option<&'a str>{ self.text.lines().find(|x| x.contains(pattern)) } pub fn find_many(&self, pattern: &str) -> Vec<&'a str>{ self.text.lines().filter(|x| x.contains(pattern)).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 { Self { text } } pub fn find_first(&'a self, word: &'a str) -> Option<&'a str> { for l in self.text.lines() { if l.contains(&word) { return Some(&l); } } return None; } pub fn find_many(&'a self, word: &'a str) -> Vec<&'a str> { self.text.lines().filter(|&l| l.contains(&word)).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>(&'a str);// 2. Implement the struct and define the methodsimpl<'a> TextFinder<'a> { pub fn new(t: &'a str) -> Self { TextFinder(t) } pub fn find_first(&self, target: &str) -> Option<&str> { self.0.lines().find(|line| line.contains(target)) } pub fn find_many(&self, target: &str) -> Vec<&str> { self.0.lines().filter(|line| line.contains(target)).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."]}
use std::io::Lines;// 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) -> TextFinder { TextFinder { text } } pub fn find_first(&self, keyword: &str) -> Option<&'a str> { self.text.lines().filter(|line| line.contains(keyword)).next() } 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}impl<'a> TextFinder<'a> { pub fn new(text: &'a str) -> TextFinder { TextFinder{text} } pub fn find_first(&self, keyword: &str) -> Option<&'a str> { self.text.lines().filter(|s| s.contains(keyword)).next() } pub fn find_many(&self, keyword: &str) -> Vec<&'a str> { 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> { text: &'a str,}// 2. Implement the struct and define the methodsimpl<'a> TextFinder<'a> { pub fn new(string: &'a str) -> Self { Self { text: string } } pub fn find_first(&self, keyword: &'a str) -> Option<&'a str> { self.text.lines().filter(|line| line.contains(keyword)).next() } 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 slice: &'a str}impl<'a> TextFinder<'a>{ pub fn new(slice: &'a str)-> Self{ Self { slice: slice } } pub fn find_first(&self, keyword: &str) -> Option<&'a str>{ self.slice.lines().filter(|line| line.contains(keyword)).next() } pub fn find_many(&self, keyword: &str) -> Vec<&'a str> { self.slice.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>(&'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, needle: &str) -> Option<&'a str> { self.0.lines().find(|line| line.contains(needle)) } pub fn find_many(&self, needle: &str) -> Vec<&'a str> { self.0.lines().filter(|line| line.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> { 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<&str> { self.text.split("\n").find(|spl| spl.contains(key)) } pub fn find_many(&self, key: &'a str) -> Vec<&str> { self.text .split("\n") .filter(|split| split.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, reference: &str) -> Option<&str> { self.text .lines() .find(|line| line.contains(reference)) } pub fn find_many(&self, reference: &str) -> Vec<&str> { self.text .lines() .filter(|line| line.contains(reference)) .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."]}
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<&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(slc: &'a str) -> Self{ TextFinder{text: slc,} } pub fn find_first(&'a self, keyword: &str) -> Option<&'a str> { self.text.lines().find(|line| line.contains(keyword)) } pub fn find_many(&'a self, key: &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,}// 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."]}
// 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, kw: &str) -> Option<&str> { let vec: Vec<&str> = self.text.split('\n').collect(); if let Some(l) = vec.iter().find(|v| v.contains(kw) ) { Some(l) } else { None } } pub fn find_many(&self, kw: &str) -> Vec<&str> { let mut v :Vec<&str> = Vec::new(); for l in self.text.split("\n") { if l.contains(kw) { v.push(l); } } 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(t: &'a str) -> Self { Self { text: t } } pub fn find_first(&self, kw: &str) -> Option<&str> { for l in self.text.split("\n") { if l.contains(kw) { return Some(l); } } None } pub fn find_many(&self, kw: &str) -> Vec<&str> { let mut v :Vec<&str> = Vec::new(); for l in self.text.split("\n") { if l.contains(kw) { v.push(l); } } 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(s: &'a str) -> Self { Self { text: s } } pub fn find_first(&self, k: &'a str) -> Option<&'a str> { self.text.lines().find(|l| l.contains(k)) } pub fn find_many(&self, k: &'a str) -> Vec<&'a str> { self.text.lines().filter(|l| l.contains(k)).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) -> TextFinder<'a> { TextFinder { text } } pub fn find_first(&self, s: &str) -> Option<&str> { self.text.lines().find(|line| line.contains(s)) } pub fn find_many(&self, s: &str) -> Vec<&str> { self.text .lines() .filter(|line| line.contains(s)) .collect::<Vec<&str>>() }}// 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> { string: &'a str,}// 2. Implement the struct and define the methodsimpl<'a> TextFinder<'a> { pub fn new(string: &'a str) -> Self { Self { string } } pub fn find_first(&self, word: &str) -> Option<&str> { self.string.lines().find(|line| line.contains(word)) } pub fn find_many(&self, word: &str) -> Vec<&str> { self.string .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> { 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<&'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> { 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().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 { Self { 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 TextFinder<'_> { pub fn new(text: &str) -> TextFinder { TextFinder{text} } pub fn find_first(&self, keyword: &str) -> Option<&str> { self.text.lines().find(|s| s.contains(keyword)) } 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,}// 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<&'a str> { self.text.split("\n").find(|i| i.contains(query)) } pub fn find_many(&self, query: &str) -> Vec<&'a str> { self.text.split("\n").filter(|i| i.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,}impl<'a> TextFinder<'a>{ pub fn new(re: &'a str) -> Self{ return Self { text: re } } pub fn find_first(&self, word: &'a str) -> Option<&'a str> { self.text .split('\n') .find(|line| line.contains(word)) } pub fn find_many(&self, word: &'a str) -> Vec<&str>{ self.text .split('\n') .filter(|line| line.contains(word)).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."]}