I'm trying to return a slice from a vector which is built inside my function. Obviously this doesn't work because v
's lifetime expires too soon. I'm wondering if there's a way to extend v
's lifetime. I want to return a plain slice, not a vector.
pub fn find<'a>(&'a self, name: &str) -> &'a[&'a Element] {
let v: Vec<&'a Element> = self.iter_elements().filter(|&elem| elem.name.borrow().local_name == name).collect();
v.as_slice()
}
See Question&Answers more detail:os