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:
.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.