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>(input: &'a Vec<String>, filter: &'a str) -> impl Iterator<Item = &'a String> { input.iter().filter(move |x| x.starts_with(filter)) }// 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.into_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>(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"]}
// Finish the functionpub fn filter_starts_with<'a>(slice: &'a Vec<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"]}
// Finish the functionpub fn filter_starts_with<'a>(slice: &'a Vec<String>, keyword: &'a str) -> impl Iterator<Item = &'a String> { slice.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], 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"]}
// Finish the functionpub fn filter_starts_with<'a>(strings: &'a Vec<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"]}
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))}// 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>(strs: &'a[String], keyword: &'a str) -> impl Iterator<Item = &'a String> { strs.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>(strs: &'a [String], keyword: &'a str) -> impl Iterator<Item = &'a String> { strs.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>(list: &'a Vec<String>, key: &'a str) -> impl Iterator<Item = &'a String> { list .iter() .filter(move |x| x.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>(strs: &'a[String], key: &'a str) -> impl Iterator<Item = &'a String> { strs.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"]}
pub fn filter_starts_with<'a>( v: &'a Vec<String>, key: &'a str,) -> impl Iterator<Item = &'a String> { v.iter().filter(move |x| x.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>( v: &'a Vec<String>, key: &'a str,) -> impl Iterator<Item = &'a String> { v.iter().filter(move |x| x.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>(v: &'a [String], k: &'a str) -> impl Iterator<Item = &'a String>{ v.iter().filter(move |x| x.starts_with(k))}// 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>(strs: &'a Vec<String>, key: &'a str) -> impl Iterator<Item = &'a String> { strs.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>(strs: &'a Vec<String>, key: &'a str) -> impl Iterator<Item = &'a String> { strs.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>(text: &'a Vec<String>, keyword: &'a str) -> impl Iterator<Item = &'a String> + 'a { text.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>(val: &'a Vec<String>, keyword: &'a str) -> impl Iterator<Item = &'a String> { val.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>( vec: &'a Vec<String>, prefix: &'a str,) -> impl Iterator<Item = &'a String> + 'a { vec.iter().filter(move |i| i.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>(vec : &'a Vec<String>, prefix: &'a str) -> impl Iterator<Item = &'a String> + 'a{ vec.iter().filter(move |i| i.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>(words: &'a[String], key: &'a str) -> impl Iterator<Item=&'a String> { words.iter().filter(move |word| word.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>(input: &'a[String], key: &'a str) -> impl Iterator<Item = &'a String> { input.iter().filter(move |e| e.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>(slice: &'a [String], start: &'a str) -> impl Iterator<Item = &'a String> { slice .iter() .filter(move |word| word.starts_with(start))}// 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],keywords:&'a str) -> impl Iterator<Item = &'a String>{ input.iter().filter(move |x| x.starts_with(keywords))}// 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],keywords:&'a str) -> impl Iterator<Item = &'a String>{ input.iter().filter(move |x| x.starts_with(keywords))}// 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 |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"]}
// Finish the functionpub fn filter_starts_with<'a>( slice: &'a [String], keyword: &'a str,) -> impl Iterator<Item = &'a String> { slice.iter().filter(move |value| value.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: 'a>(slice: &'a Vec<String>, keyword: &'b str) -> impl Iterator<Item = &'a String> + 'a { slice.into_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 Vec<String>, key: &'a str) -> impl Iterator<Item = &'a String> { input.iter().filter(move |&e| e.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 Vec<String>, key: &'a str) -> impl Iterator<Item = &'a String> { input.iter().filter(move |&e| e.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 Vec<String>, key: &'a str) -> impl Iterator<Item = &'a String> { input.iter().filter(move |e| e.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>(sl: &'a Vec<String>, k: &'a str) -> impl Iterator<Item = &'a String> { sl.iter() .filter(move |x| x.starts_with(k))}// 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], k: &'a str) -> impl Iterator<Item = &'a String>{ v.iter().filter(move |x| x.starts_with(k))}// 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], x: &'a str) -> impl Iterator<Item = &'a String>{ v.iter().filter(move |w| w.starts_with(x))}// 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>(items: &'a [String], k: &'a str) -> impl Iterator<Item = &'a String> { items.iter().filter(move |x| x.starts_with(k))}// 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 |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], key: &'a str,) -> impl Iterator<Item = &'a String> { slice.iter().filter(move |x| x.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>(items: &'a [String], key: &'a str) -> impl Iterator<Item=&'a String> { items.iter().filter(move |x| x.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], keyword: &'a str) -> impl Iterator<Item=&'a String> { strings.iter() .filter(move |temp| temp.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 function// pub 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 usage// 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 [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>(sl: &'a [String], keyword: &'a str) -> impl Iterator<Item = &'a String> { sl.iter().filter(move |val| val.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 |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 |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"]}
// to declare that `impl Iterator` captures `'_`, you can introduce a named lifetime parameter `'a`pub 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>(haystack: &'a Vec<String>, needle: &'a str) -> impl Iterator<Item=&'a String> { haystack.iter().filter(move |x| x.starts_with(needle))}// 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>(haystack: &'a Vec<String>, needle: &'a str) -> impl Iterator<Item=&'a String> { haystack.iter().filter(move |x| x.starts_with(needle))}// 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>(haystack: &'a Vec<String>, needle: &'a str) -> impl Iterator<Item=&'a String> { haystack.iter() .filter(move |x| x.starts_with(needle))}// 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 Vec<String>, start: &'a str) -> impl Iterator<Item=&'a String> { list.iter().filter(move |s| s.starts_with(start))}// 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 function//pub fn filter_starts_with (list: &Vec<String>, keyword: &str) -> impl Iterator<Item=&String> + '_ { pub fn filter_starts_with<'a> (list: &'a Vec<String>, keyword: &'a str) -> impl Iterator<Item=&'a String> + 'a { // let mut vec: Vec::<_> = vec![]; //let filteredList: Vec<_> = list.iter() list.iter() .filter(move |item | item.starts_with(keyword))//.collect::<Vec<String>>(); //.collect::<Vec<_>>() // .into_iter(); //filteredList.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"]}