I have a method as below:
pub fn filter_names(&self, query: &str, starts_with: bool) -> Box<dyn Iterator<Item = &str>> {
debug!("Filtering code names...");
trace!("query: {}", query);
trace!("starts with: {}", starts_with);
debug!("Generating code iterator...");
Box::new(self
.codes
.iter()
.filter(|c| match starts_with {
true => {
let do_filter = c.name.starts_with(query);
trace!("`{}` starts with `{}`: {}", c.name, query, do_filter);
do_filter
}
false => {
let do_filter = c.name.contains(query);
trace!("`{}` contains `{}`: {}", c.name, query, do_filter);
do_filter
}
})
.map(|c| {
debug!("Mapping `{}` to &str...", c.name);
c.name
}))
}
I do not like to collect it and leave collecting to library users. I don't know if this is a valid approach in Rust, though it is in Python. Lazy evaluation matters in cases which the performance is desired. It is the logic behind.
This code, however, fails on iter()
call on self
saying:
cannot infer an appropriate lifetime for autoref due to conflicting requirements
note: but, the lifetime must be valid for the static lifetime...
note: expected `std::boxed::Box<(dyn std::iter::Iterator<Item = &str> + 'static)>`
found `std::boxed::Box<dyn std::iter::Iterator<Item = &str>>`rustc(E0495)
Why does compiler expect that the return of filter_names
is supposed to be 'static
in this case? What is a viable solution to this issue? AFAIK, 'static
lives as long as the program lives, which, I don't think, is not desirable because I do not want this Iterator
trait object to live as long as the program runs, I want it to be called and let the Iterator
trait object live as much as the scope that calls it needs it.
Thanks in advance.
Environment
- Rust 1.43.1