I’m learning Rust, and I wrote the following code to extract useful data from HTML documents with the select.rs crate.
let document = Document::from(&*html);
for node in document.find(Name("a")).iter() {
let anchor = node.text().trim();
match node.attr("href") {
Some(href) => {
if anchor.len() > 0 {
println!("Anchor: {}, Link: ({:?})", anchor, href);
} else {
println!("Anchor: [No anchor], Link: ({:?})", href);
}
}
None => (),
}
}
src\main.rs:29:38: 29:49 error: borrowed value does not live long enough
src\main.rs:29 let anchor = node.text().trim();
This confuses me. Is it text() or trim() that doesn’t live long enough?
I do this:
for node in document.find(Name("a")).iter() {
let anchor = node.text();
let anchor = anchor.trim();
This worked fine, but is it good hint?