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> { 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."]}
// 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, substr: &str) -> Option<&str> { self.text.lines().find(|l| l.contains(substr)) } pub fn find_many(&self, substr: &str) -> Vec<&str> { self.text.lines().filter(|l| l.contains(substr)).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: &'a str) -> Option<&'a str> { let vector: Vec<&str> = self.text.split("\n").collect(); if let Some(line) = vector.iter().find(|val| val.contains(word)) { return Some(line); } None } pub fn find_many(&self, word: &'a str) -> Vec<&str> { let vector: Vec<&str> = self.text.split("\n").collect(); let mut lines:Vec<&str> = vec![]; vector.iter().for_each(|val| { if val.contains(word) { lines.push(val); } }); lines }}// 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: &'a str) -> Option<&'a str> { let vector: Vec<&str> = self.text.split("\n").collect(); if let Some(line) = vector.iter().find(|val| val.contains(word)) { return Some(line); } None } pub fn find_many(&self, word: &'a str) -> Vec<&str> { let vector: Vec<&str> = self.text.split("\n").collect(); let mut lines:Vec<&str> = vec![]; vector.iter().for_each(|val| { if val.contains(word) { lines.push(val); } }); lines }}// 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: text } } pub fn find_first(&self, key: &str) -> Option<&str> { self.text.lines().find(|l| l.contains(key)) } pub fn find_many(&self, key: &str) -> Vec<&str> { self.text.lines().filter(|l| l.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> { pub text: &'a str,}impl TextFinder<'_> { pub fn new<'a>(text: &'a str) -> TextFinder<'a> { TextFinder { text } } pub fn find_first<'a>(&'a self, key: &str) -> Option<&'a str> { let mut lines = self.text.lines(); while let Some(l) = lines.next() { if l.contains(key) { return Some(l) } } None } pub fn find_many<'a>(&'a self, key: &str) -> Vec<&'a str> { let mut lines = self.text.lines(); let mut matches = vec![]; while let Some(l) = lines.next() { if l.contains(key) { matches.push(l); } } matches }}// 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>{ content : &'a str,}// 2. Implement the struct and define the methodsimpl<'a> TextFinder<'a> { pub fn new(content: &'a str) -> Self { TextFinder { content } } pub fn find_first(&self, text: &str) -> Option<&str> { for line in self.content.lines() { if line.contains(text) { return Some(line); } } None } pub fn find_many(&self, text: &str) -> Vec<&str> { let mut results = Vec::new(); for line in self.content.lines() { if line.contains(text) { results.push(line); } } results }}// 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 TextFinder<'_> { pub fn new (text: &str) -> TextFinder { TextFinder { 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(text: &'a str) -> Self { Self { text } } pub fn find_first(&self, keyword: &str) -> Option<&str> { self.text.lines().find(|l| l.contains(&keyword) ) } pub fn find_many(&self, keyword: &str) -> Vec<&str> { self.text.lines().filter(|l| l.contains(&keyword) ).collect() }}// Example usagepub fn main() { let text = "Rust is fast and memory-efficient.\nOwnership is key to Rust's safety.\nRustaceans love the borrow checker."; let finder = TextFinder::new(text); let first = finder.find_first("Rust"); println!("{:?}", first); // Should print: Some("Rust is fast and memory-efficient.") let matches = finder.find_many("Rust"); println!("{:?}", matches); // Should print: ["Rust is fast and memory-efficient.", "Ownership is key to Rust's safety."]}
// 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, keyword: &str) -> Option<&'a str> { self.slice.lines().find(|x| x.contains(keyword)) } pub fn find_many(&self, keyword: &str) -> Vec<&'a str> { self.slice.lines().filter(|x| x.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> { 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, string: &'a str) -> Option<&'a str> { for line in self.string.lines() { if line.contains(string) { return Some(line); } } None } pub fn find_many(&self, string: &'a str) -> Vec<&'a str> { let mut vector = Vec::new(); for line in self.string.lines() { if line.contains(string) { vector.push(line); } } vector }}// 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 } } // No need to add lifetime due to elision rules pub fn find_first(&self, kw: &str) -> Option<&str> { self.text.lines().find(|line| line.contains(kw)) } // No need to add lifetime due to elision rules pub fn find_many(&self, kw: &str) -> Vec<&str> { self.text.lines().filter(|line| line.contains(kw)).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, key: &str) -> Option<&'a str> { self.text.lines().find(|line| line.contains(key)) } pub fn find_many(&self, key: &str) -> Vec<&'a str> { self.text .lines() .filter(|line| line.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}impl<'a> TextFinder<'a> { pub fn new(text: &'a str) -> Self { TextFinder{text} } pub fn find_first(&self, keyword: &'a str) -> Option<&'a str> { self.text .lines() .find(|x| x.contains(keyword)) } pub fn find_many(&self, keyword: &'a 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."]}
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> { text: &'a str}impl<'a> TextFinder<'a> { pub fn new(text: &'a str) -> Self { TextFinder{text} } pub fn find_first(&self, search: &str) -> Option<&'a str> { self.text.lines().find(|x| x.contains(search)) } pub fn find_many(&self, search: &str) -> Vec<&'a str> { let result: Vec<&str> = self.text.lines().filter(|x| x.contains(search)).collect(); 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<'a>{ pub text : &'a str,}impl<'a> TextFinder<'a>{ pub fn new(t : &'a str) -> Self{ return Self{ text : t, }; } pub fn find_first(&self,t : &str) -> Option<&'a str>{ return self.text .lines() .find(move |e| e.contains(t)); } pub fn find_many(&self,t : &str) -> Vec<&'a str>{ return self.text .lines() .filter(move |e| e.contains(t)) .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 { Self { 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> { 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, a: &str) -> Option<&str> { self.slice.lines().find(|s| s.contains(a)) } pub fn find_many(&self, a: &str) -> Vec<&str> { self.slice.lines().filter(|s| s.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,}// 2. Implement the struct and define the methodsimpl<'a> TextFinder<'a> { pub fn new(slice : &'a str) -> Self { Self { text : slice } } pub fn find_first(&self, slice : &str) -> Option<& str>{ self.text.lines().find(|l| l.contains(&slice)) } pub fn find_many(&self, slice : &str) -> Vec<& str>{ self.text.lines().filter(|l| l.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."]}