I'm trying to implement a method (sel
) for an external (scraper::Html
) struct. But compiler won't let me, because he doesn't know the lifetime of the local &select
borrow. I guess I should somehow make it 'a
, but I can't figure out how to do this. For now I could only collect nodes into Vec
in sel_vec
, but it doesn't feel quite idiomatic. Any suggestions?
use scraper::{html::Select, ElementRef, Html, Selector};
trait Sel {
fn sel<'a, 'b>(&'a self, selector: &'b str) -> Select<'a, 'b>;
fn sel_vec<'a, 'b>(&'a self, selector: &'b str) -> Vec<ElementRef<'_>>;
}
impl Sel for Html {
fn sel<'a, 'b>(&'a self, selector: &'b str) -> Select<'a, 'b> {
let selector = Selector::parse(selector).unwrap();
// rustc: cannot return value referencing local variable `selector`
self.select(&selector)
}
fn sel_vec<'a, 'b>(&'a self, selector: &'b str) -> Vec<ElementRef<'_>> {
let selector = Selector::parse(selector).unwrap();
self.select(&selector).collect()
}
}