Rust's impl Trait
syntax allows for concise and flexible code, especially when returning iterators or other complex types. By using impl Trait
, you can abstract away concrete types while ensuring optimal performance through Rust's zero-cost abstractions.
In this challenge, you will implement a function that filters strings from a slice, returning only those that start with a given keyword. The function will return an iterator over the filtered results. This approach demonstrates how to combine Rust's iterator combinators with the impl Trait
syntax.
Write a function named filter_starts_with
that:
String
&str
keyword.iter()
to iterate over references to the strings in the slice.filter
method takes a closure to apply a filtering condition.starts_with
method to check if a string starts with a keyword.move
in the closure to capture the keyword.// Finish the functionpub fn filter_starts_with<'a>(strings: &'a[String], keyword: &'a str) -> impl std::iter::Iterator<Item = &'a String> { strings.iter().filter(move |s| s.starts_with(keyword))}// Example usagepub fn main() { let input = vec![ String::from("apple"), String::from("apricot"), String::from("banana"), String::from("cherry"), ]; let filtered: Vec<&String> = filter_starts_with(&input, "ap").collect(); println!("{:?}", filtered); // Expected output: ["apple", "apricot"]}
// Finish the functionpub fn filter_starts_with<'a>(v: & 'a Vec<String>, s: &'a str) -> impl Iterator<Item = &'a String> + 'a{v.iter().filter(move |x| x.starts_with(s) )}// Example usagepub fn main() { let input = vec![ String::from("apple"), String::from("apricot"), String::from("banana"), String::from("cherry"), ]; let filtered: Vec<&String> = filter_starts_with(&input, "ap").collect(); println!("{:?}", filtered); // Expected output: ["apple", "apricot"]}
// Finish the functionpub fn filter_starts_with<'a>(input: &'a Vec<String>, filter: &'a str) -> Box<dyn Iterator<Item = &'a String> + 'a> { let iter = input.iter().filter(move |word| word.starts_with(filter)); Box::new(iter)}// Example usagepub fn main() { let input = vec![ String::from("apple"), String::from("apricot"), String::from("banana"), String::from("cherry"), ]; let filtered: Vec<&String> = filter_starts_with(&input, "ap").collect(); println!("{:?}", filtered); // Expected output: ["apple", "apricot"]}
pub fn filter_starts_with<'a>( i: &'a Vec<String>, filter: &'a str) -> Box<dyn Iterator<Item = &'a String> + 'a> { let iter = i.iter().filter(move |v| v.starts_with(filter)); Box::new(iter)}// Example usagepub fn main() { let input = vec![ String::from("apple"), String::from("apricot"), String::from("banana"), String::from("cherry"), ]; let filtered: Vec<&String> = filter_starts_with(&input, "ap").collect(); println!("{:?}", filtered); // Expected output: ["apple", "apricot"]}
// Finish the functionpub fn filter_starts_with<'a>(slice: &'a[String], key: &'a str) -> impl Iterator<Item = &'a String> { slice.iter() .filter(move |s| s.starts_with(key))}// Example usagepub fn main() { let input = vec![ String::from("apple"), String::from("apricot"), String::from("banana"), String::from("cherry"), ]; let filtered: Vec<&String> = filter_starts_with(&input, "ap").collect(); println!("{:?}", filtered); // Expected output: ["apple", "apricot"]}
// Finish the functionpub fn filter_starts_with<'a>( strings: &'a [String], prefix: &'a str,) -> impl Iterator<Item = &'a String> { strings .iter() .filter(move |s| s.starts_with(prefix))}// Example usagepub fn main() { let input = vec![ String::from("apple"), String::from("apricot"), String::from("banana"), String::from("cherry"), ]; let filtered: Vec<&String> = filter_starts_with(&input, "ap").collect(); println!("{:?}", filtered); // Expected output: ["apple", "apricot"]}
// Finish the functionpub fn filter_starts_with<'a>(input: &'a [String], key: &'a str) -> impl Iterator<Item = &'a String> { input.iter().filter(move |elem| elem.starts_with(key))}// Example usagepub fn main() { let input = vec![ String::from("apple"), String::from("apricot"), String::from("banana"), String::from("cherry"), ]; let filtered: Vec<&String> = filter_starts_with(&input, "ap").collect(); println!("{:?}", filtered); // Expected output: ["apple", "apricot"]}
// Finish the functionpub fn filter_starts_with<'a>(arr: &'a [String], prefix: &'a str) -> impl Iterator<Item = &'a String>{ // change of ownership from vector using move arr.iter().filter(move |letter| letter.starts_with(prefix))}// Example usagepub fn main() { let input = vec![ String::from("apple"), String::from("apricot"), String::from("banana"), String::from("cherry"), ]; // because the collection occurs out of func lifetime let filtered: Vec<&String> = filter_starts_with(&input, "ap").collect(); println!("{:?}", filtered); // Expected output: ["apple", "apricot"]}
// Finish the functionpub fn filter_starts_with<'a>(mass: &'a [String], prefix: &'a str) -> impl Iterator<Item = &'a String> { mass.iter().filter(move |x| {x.starts_with(prefix)})}// Example usagepub fn main() { let input = vec![ String::from("apple"), String::from("apricot"), String::from("banana"), String::from("cherry"), ]; let filtered: Vec<&String> = filter_starts_with(&input, "ap").collect(); println!("{:?}", filtered); // Expected output: ["apple", "apricot"]}
pub fn filter_starts_with<'a>(words: &'a [String], keyword: &'a str) -> impl Iterator<Item=&'a String>{ words.iter().filter(move |x| x.starts_with(&keyword))}// Example usagepub fn main() { let input = vec![ String::from("apple"), String::from("apricot"), String::from("banana"), String::from("cherry"), ]; let filtered: Vec<&String> = filter_starts_with(&input, "ap").collect(); println!("{:?}", filtered); // Expected output: ["apple", "apricot"]}
// Finish the functionpub fn filter_starts_with<'a>(inputs: &'a [String], target: &'a str) -> impl Iterator<Item = &'a String> { inputs.iter().filter(move |s| s.starts_with(target))}// Example usagepub fn main() { let input = vec![ String::from("apple"), String::from("apricot"), String::from("banana"), String::from("cherry"), ]; let filtered: Vec<&String> = filter_starts_with(&input, "ap").collect(); println!("{:?}", filtered); // Expected output: ["apple", "apricot"]}
// Finish the functionpub fn filter_starts_with<'a>(v: &'a [String], keyword: &'a str) -> impl Iterator<Item=&'a String> { v.iter().filter(move |s| s.starts_with(&keyword))}// Example usagepub fn main() { let input = vec![ String::from("apple"), String::from("apricot"), String::from("banana"), String::from("cherry"), ]; let filtered: Vec<&String> = filter_starts_with(&input, "ap").collect(); println!("{:?}", filtered); // Expected output: ["apple", "apricot"]}
// Finish the functionpub fn filter_starts_with<'a>(words: &'a Vec<String>, keyword: &'a str) -> impl Iterator<Item = &'a String> { words.iter().filter(move |w| w.starts_with(&keyword))}// Example usagepub fn main() { let input = vec![ String::from("apple"), String::from("apricot"), String::from("banana"), String::from("cherry"), ]; let filtered: Vec<&String> = filter_starts_with(&input, "ap").collect(); println!("{:?}", filtered); // Expected output: ["apple", "apricot"]}
// Finish the functionpub fn filter_starts_with<'a>(s: &'a Vec<String>, keyword: &'a str) -> impl Iterator<Item = &'a String> { s.iter().filter(move |w| w.starts_with(&keyword))}// Example usagepub fn main() { let input = vec![ String::from("apple"), String::from("apricot"), String::from("banana"), String::from("cherry"), ]; let filtered: Vec<&String> = filter_starts_with(&input, "ap").collect(); println!("{:?}", filtered); // Expected output: ["apple", "apricot"]}
// Finish the functionpub fn filter_starts_with<'a>( slice: &'a [String], keyword: &'a str,) -> impl Iterator<Item = &'a String> { slice .iter() .filter(move |string| string.starts_with(keyword))}// Example usagepub fn main() { let input = vec![ String::from("apple"), String::from("apricot"), String::from("banana"), String::from("cherry"), ]; let filtered: Vec<&String> = filter_starts_with(&input, "ap").collect(); println!("{:?}", filtered); // Expected output: ["apple", "apricot"]}
// Finish the functionpub fn filter_starts_with<'a>(s: &'a Vec<String>, keyword: &'a str) -> impl Iterator<Item = &'a String> { s.iter().filter(move |w| w.starts_with(&keyword))}// Example usagepub fn main() { let input = vec![ String::from("apple"), String::from("apricot"), String::from("banana"), String::from("cherry"), ]; let filtered: Vec<&String> = filter_starts_with(&input, "ap").collect(); println!("{:?}", filtered); // Expected output: ["apple", "apricot"]}
// Finish the functionpub fn filter_starts_with<'a>( slicee: &'a [String], key: &'a str,) -> impl Iterator<Item = &'a String> { slicee.iter().filter( move|item| item.starts_with(key))}// Example usagepub fn main() { let input = vec![ String::from("apple"), String::from("apricot"), String::from("banana"), String::from("cherry"), ]; let filtered: Vec<&String> = filter_starts_with(&input, "ap").collect(); println!("{:?}", filtered); // Expected output: ["apple", "apricot"]}
// Finish the functionpub fn filter_starts_with<'a>( item: &'a [String], key: &'a str,) -> impl Iterator<Item = &'a String> { item.iter().filter(move |item| item.starts_with(key))}// Example usagepub fn main() { let input = vec![ String::from("apple"), String::from("apricot"), String::from("banana"), String::from("cherry"), ]; let filtered: Vec<&String> = filter_starts_with(&input, "ap").collect(); println!("{:?}", filtered); // Expected output: ["apple", "apricot"]}
// Finish the functionpub fn filter_starts_with<'a>(input: &'a [String], prefix: &'a str) -> impl Iterator<Item = &'a String> { input.iter().filter(move |s| s.starts_with(prefix))}// Example usagepub fn main() { let input = vec![ String::from("apple"), String::from("apricot"), String::from("banana"), String::from("cherry"), ]; let filtered: Vec<&String> = filter_starts_with(&input, "ap").collect(); println!("{:?}", filtered); // Expected output: ["apple", "apricot"]}
// Finish the functionpub fn filter_starts_with<'a>(slice: &'a [String], keyword: &'a str) -> impl Iterator<Item = &'a String> { slice.iter().filter(move |s| s.starts_with(keyword))}// Example usagepub fn main() { let input = vec![ String::from("apple"), String::from("apricot"), String::from("banana"), String::from("cherry"), ]; let filtered: Vec<&String> = filter_starts_with(&input, "ap").collect(); println!("{:?}", filtered); // Expected output: ["apple", "apricot"]}
pub fn filter_starts_with<'a>(slice: &'a [String], keyword: &'a str) -> impl Iterator<Item = &'a String> { slice.iter().filter(move |s| s.starts_with(keyword))}pub fn main() { let input = vec![ String::from("apple"), String::from("apricot"), String::from("banana"), String::from("cherry"), ]; let filtered: Vec<&String> = filter_starts_with(&input, "ap").collect(); println!("{:?}", filtered); // Expected output: ["apple", "apricot"]}
pub fn filter_starts_with<'a>(slice: &'a Vec<String>, keyword: &'a str) -> impl Iterator<Item = &'a String> { slice.iter().filter(move |s| s.starts_with(keyword))}pub fn main() { let input = vec![ String::from("apple"), String::from("apricot"), String::from("banana"), String::from("cherry"), ]; let filtered: Vec<&String> = filter_starts_with(&input, "ap").collect(); println!("{:?}", filtered); // Expected output: ["apple", "apricot"]}
pub fn filter_starts_with<'a>( input: &'a [String], keyword: &'a str,) -> impl Iterator<Item = &'a String> { input.iter().filter(move |el| el.starts_with(keyword))}// Example usagepub fn main() { let input = vec![ String::from("apple"), String::from("apricot"), String::from("banana"), String::from("cherry"), ]; let filtered: Vec<&String> = filter_starts_with(&input, "ap").collect(); println!("{:?}", filtered); // Expected output: ["apple", "apricot"]}
// Finish the functionpub fn filter_starts_with<'a>(slices: &'a[String], keyword: &'a str) -> impl Iterator<Item = &'a String> { slices.iter().filter(move |v| v.starts_with(keyword))}// Example usagepub fn main() { let input = vec![ String::from("apple"), String::from("apricot"), String::from("banana"), String::from("cherry"), ]; let filtered: Vec<&String> = filter_starts_with(&input, "ap").collect(); println!("{:?}", filtered); // Expected output: ["apple", "apricot"]}
// Finish the functionpub fn filter_starts_with<'a>(input: &'a [String], keyword: &'a str) -> impl Iterator<Item = &'a String> { input.iter().filter(move |element| element.starts_with(keyword))}// Example usagepub fn main() { let input = vec![ String::from("apple"), String::from("apricot"), String::from("banana"), String::from("cherry"), ]; let filtered: Vec<&String> = filter_starts_with(&input, "ap").collect(); println!("{:?}", filtered); // Expected output: ["apple", "apricot"]}
// Finish the functionpub fn filter_starts_with<'a>(input: &'a [String], ptrn: &'a str) -> impl std::iter::Iterator<Item = &'a String> { input.iter().filter(move |s| s.starts_with(ptrn))}// Example usagepub fn main() { let input = vec![ String::from("apple"), String::from("apricot"), String::from("banana"), String::from("cherry"), ]; let filtered: Vec<&String> = filter_starts_with(&input, "ap").collect(); println!("{:?}", filtered); // Expected output: ["apple", "apricot"]}
// Finish the functionpub fn filter_starts_with<'a>(arr: &'a [String], val: &'a str) -> impl Iterator<Item = &'a String> { arr.iter().filter(move |s| s.starts_with(val))}// Example usagepub fn main() { let input = vec![ String::from("apple"), String::from("apricot"), String::from("banana"), String::from("cherry"), ]; let filtered: Vec<&String> = filter_starts_with(&input, "ap").collect(); println!("{:?}", filtered); // Expected output: ["apple", "apricot"]}
// Finish the functionpub fn filter_starts_with<'a>(data: &'a[String], keyword: &'a str) -> impl Iterator<Item = &'a String> { data.into_iter().filter(move |&s| s.starts_with(keyword)).into_iter()}// Example usagepub fn main() { let input = vec![ String::from("apple"), String::from("apricot"), String::from("banana"), String::from("cherry"), ]; let filtered: Vec<&String> = filter_starts_with(&input, "ap").collect(); println!("{:?}", filtered); // Expected output: ["apple", "apricot"]}
// Finish the functionpub fn filter_starts_with<'a>(slice: &'a [String], keyword: &'a str) -> impl Iterator<Item = &'a String> { slice.iter().filter(move |st| st.starts_with(keyword))}// Example usagepub fn main() { let input = vec![ String::from("apple"), String::from("apricot"), String::from("banana"), String::from("cherry"), ]; let filtered: Vec<&String> = filter_starts_with(&input, "ap").collect(); println!("{:?}", filtered); // Expected output: ["apple", "apricot"]}
// Finish the functionpub fn filter_starts_with<'a>(src: &'a [String], keyword: &'a str) -> impl Iterator<Item=&'a String>{ src.iter().filter(move |e| e.starts_with(keyword))}// Example usagepub fn main() { let input = vec![ String::from("apple"), String::from("apricot"), String::from("banana"), String::from("cherry"), ]; let filtered: Vec<&String> = filter_starts_with(&input, "ap").collect(); println!("{:?}", filtered); // Expected output: ["apple", "apricot"]}
// Finish the functionpub fn filter_starts_with<'a>(slice: &'a [String], prefix: &'a str) -> impl Iterator<Item = &'a String> + 'a { slice.iter() .filter(move |s| s.starts_with(prefix))}// Example usagepub fn main() { let input = vec![ String::from("apple"), String::from("apricot"), String::from("banana"), String::from("cherry"), ]; let filtered: Vec<&String> = filter_starts_with(&input, "ap").collect(); println!("{:?}", filtered); // Expected output: ["apple", "apricot"]}
// Finish the functionpub fn filter_starts_with<'a>(hay: &'a [String], key: &'a str) -> impl Iterator<Item = &'a String> + 'a { hay.iter() .filter(move |s| s.starts_with(key))}// Example usagepub fn main() { let input = vec![ String::from("apple"), String::from("apricot"), String::from("banana"), String::from("cherry"), ]; let filtered: Vec<&String> = filter_starts_with(&input, "ap").collect(); println!("{:?}", filtered); // Expected output: ["apple", "apricot"]}
pub fn filter_starts_with<'a>( slice: &'a [String], keyword: &'a str,) -> impl Iterator<Item = &'a String> { slice.iter().filter(move |x| x.starts_with(keyword))}// Example usagepub fn main() { let input = vec![ String::from("apple"), String::from("apricot"), String::from("banana"), String::from("cherry"), ]; let filtered: Vec<&String> = filter_starts_with(&input, "ap").collect(); println!("{:?}", filtered); // Expected output: ["apple", "apricot"]}
pub fn filter_starts_with<'a>(s: &'a [String], kw: &'a str) -> impl Iterator<Item = &'a String> { s.iter().filter(move |&s| s.starts_with(kw))}// Example usagepub fn main() { let input = vec![ String::from("apple"), String::from("apricot"), String::from("banana"), String::from("cherry"), ]; let filtered: Vec<&String> = filter_starts_with(&input, "ap").collect(); println!("{:?}", filtered); // Expected output: ["apple", "apricot"]}
// Finish the functionpub fn filter_starts_with<'a>(strings: &'a [String], keyword: &'a str) -> impl Iterator<Item = &'a String> { strings .iter() .filter(move |s| s.starts_with(keyword))}// Example usagepub fn main() { let input = vec![ String::from("apple"), String::from("apricot"), String::from("banana"), String::from("cherry"), ]; let filtered: Vec<&String> = filter_starts_with(&input, "ap").collect(); println!("{:?}", filtered); // Expected output: ["apple", "apricot"]}
// Finish the functionpub fn filter_starts_with<'a>( strings: &'a [String], keyword: &'a str,) -> impl Iterator<Item = &'a String> { strings.iter().filter(move |s| s.starts_with(keyword))}// Example usagepub fn main() { let input = vec![ String::from("apple"), String::from("apricot"), String::from("banana"), String::from("cherry"), ]; let filtered: Vec<&String> = filter_starts_with(&input, "ap").collect(); println!("{:?}", filtered); // Expected output: ["apple", "apricot"]}
// Finish the functionpub fn filter_starts_with<'a>(v: &'a [String], prefix: &'a str) -> impl Iterator<Item = &'a String> { v.iter().filter(move |s| s.starts_with(prefix))}// Example usagepub fn main() { let input = vec![ String::from("apple"), String::from("apricot"), String::from("banana"), String::from("cherry"), ]; let filtered: Vec<&String> = filter_starts_with(&input, "ap").collect(); println!("{:?}", filtered); // Expected output: ["apple", "apricot"]}
pub fn filter_starts_with<'a>(values: &'a [String], prefix: &'a str) -> impl Iterator<Item = &'a String> { values.iter().filter(move |v| v.starts_with(prefix))}pub fn main() { let input = vec![ String::from("apple"), String::from("apricot"), String::from("banana"), String::from("cherry"), ]; let filtered: Vec<&String> = filter_starts_with(&input, "ap").collect(); println!("{:?}", filtered); // Expected output: ["apple", "apricot"]}
pub fn filter_starts_with<'a>(values: &'a [String], prefix: &'a str) -> impl Iterator<Item = &'a String> { values.iter().filter(move |v| v.starts_with(prefix))}pub fn main() { let input = vec![ String::from("apple"), String::from("apricot"), String::from("banana"), String::from("cherry"), ]; let filtered: Vec<&String> = filter_starts_with(&input, "ap").collect(); println!("{:?}", filtered); // Expected output: ["apple", "apricot"]}
// Finish the functionpub fn filter_starts_with<'a>(slice: &'a Vec<String>, word: &'a str) -> impl Iterator<Item = &'a String> + 'a { slice.iter().filter(move |text| text.starts_with(word))}// Example usagepub fn main() { let input = vec![ String::from("apple"), String::from("apricot"), String::from("banana"), String::from("cherry"), ]; let filtered: Vec<&String> = filter_starts_with(&input, "ap").collect(); println!("{:?}", filtered); // Expected output: ["apple", "apricot"]}
use std::vec;// Finish the functionpub fn filter_starts_with<'a>(list: &'a Vec<String>, keyword: &'a str) -> impl Iterator<Item=&'a String> { list.iter().filter(move |x| x.starts_with(keyword))}// Example usagepub fn main() { let input = vec![ String::from("apple"), String::from("apricot"), String::from("banana"), String::from("cherry"), ]; let filtered: Vec<&String> = filter_starts_with(&input, "ap").collect(); println!("{:?}", filtered); // Expected output: ["apple", "apricot"]}
// Finish the functionpub fn filter_starts_with<'a>(list: &'a [String], keyword: &'a str) -> impl Iterator<Item = &'a String>{ list.iter().filter(move |s| s.starts_with(keyword))}// Example usagepub fn main() { let input = vec![ String::from("apple"), String::from("apricot"), String::from("banana"), String::from("cherry"), ]; let filtered: Vec<&String> = filter_starts_with(&input, "ap").collect(); println!("{:?}", filtered); // Expected output: ["apple", "apricot"]}
// Finish the functionpub fn filter_starts_with<'a>(strings: &'a[String], keyword: &'a str) -> impl Iterator<Item = &'a String> { strings.iter().filter(move |v| v.starts_with(keyword))}// Example usagepub fn main() { let input = vec![ String::from("apple"), String::from("apricot"), String::from("banana"), String::from("cherry"), ]; let filtered: Vec<&String> = filter_starts_with(&input, "ap").collect(); println!("{:?}", filtered); // Expected output: ["apple", "apricot"]}
// Finish the functionpub fn filter_starts_with<'a: 'b, 'b>(slice: &'a [String], keyword: &'b str) -> impl Iterator<Item = &'a String> + 'b { slice.iter() .filter(move |s| s.starts_with(keyword))}// Example usagepub fn main() { let input = vec![ String::from("apple"), String::from("apricot"), String::from("banana"), String::from("cherry"), ]; let filtered: Vec<&String> = filter_starts_with(&input, "ap").collect(); println!("{:?}", filtered); // Expected output: ["apple", "apricot"]}
// Finish the functionpub fn filter_starts_with<'a>(strings: &'a[String], keyword: &'a str) -> impl Iterator<Item = &'a String> { strings.iter().filter(move |v| v.starts_with(keyword))}// Example usagepub fn main() { let input = vec![ String::from("apple"), String::from("apricot"), String::from("banana"), String::from("cherry"), ]; let filtered: Vec<&String> = filter_starts_with(&input, "ap").collect(); println!("{:?}", filtered); // Expected output: ["apple", "apricot"]}
// Finish the functionpub fn filter_starts_with<'a>(strings: &'a [String], keyword: &'a str) -> impl Iterator<Item = &'a String> + 'a{ strings.iter().filter(move |s| s.starts_with(keyword))}// Example usagepub fn main() { let input = vec![ String::from("apple"), String::from("apricot"), String::from("banana"), String::from("cherry"), ]; let filtered: Vec<&String> = filter_starts_with(&input, "ap").collect(); println!("{:?}", filtered); // Expected output: ["apple", "apricot"]}
// Finish the functionpub fn filter_starts_with<'a>(strings: &'a [String], keyword: &'a str) -> impl Iterator<Item = &'a String> + 'a{ strings.iter().filter(move |s| s.starts_with(keyword))}// Example usagepub fn main() { let input = vec![ String::from("apple"), String::from("apricot"), String::from("banana"), String::from("cherry"), ]; let filtered: Vec<&String> = filter_starts_with(&input, "ap").collect(); println!("{:?}", filtered); // Expected output: ["apple", "apricot"]}
// Finish the functionpub fn filter_starts_with<'a>(word: &'a [String], keyword: &'a str) -> impl Iterator<Item = &'a String> { word.iter().filter(move |w| w.starts_with(keyword))}// Example usagepub fn main() { let input = vec![ String::from("apple"), String::from("apricot"), String::from("banana"), String::from("cherry"), ]; let filtered: Vec<&String> = filter_starts_with(&input, "ap").collect(); println!("{:?}", filtered); // Expected output: ["apple", "apricot"]}
// Finish the functionpub fn filter_starts_with<'a>(slice: &'a [String], keyword: &'a str) -> impl Iterator<Item = &'a String> + 'a { slice.iter().filter(move |x| x.starts_with(keyword))}// Example usagepub fn main() { let input = vec![ String::from("apple"), String::from("apricot"), String::from("banana"), String::from("cherry"), ]; let filtered: Vec<&String> = filter_starts_with(&input, "ap").collect(); println!("{:?}", filtered); // Expected output: ["apple", "apricot"]}
// Finish the functionpub fn filter_starts_with<'a>( slice: &'a Vec<String>, keyword: &'a str,) -> impl Iterator<Item = &'a String> { slice.iter().filter(move |s| s.starts_with(keyword))}// Example usagepub fn main() { let input = vec![ String::from("apple"), String::from("apricot"), String::from("banana"), String::from("cherry"), ]; let filtered: Vec<&String> = filter_starts_with(&input, "ap").collect(); println!("{:?}", filtered); // Expected output: ["apple", "apricot"]}