I wrote a example,and run without compiler error
use std::collections::HashSet;
use std::error::Error;
use html5ever::rcdom;
use html5ever::rcdom::Handle;
use reqwest;
use soup::{prelude, QueryBuilder};
use soup::prelude::*;
use testquestion::testtrait::Test;
fn main() -> Result<(), Box<Error>> {
let resp = reqwest::get("https://docs.rs/soup/0.1.0/soup/")?;
let soup = Soup::from_reader(resp)?;
let result = soup
.tag("section")
.attr("id", "main")
.find()
.and_then(|section:Handle| -> Option<String> {
section
.tag("span")
.attr("class", "in-band")
.find()
.map(|span:Handle| -> String {
(&span as &rcdom::Node).text();
(&span as &Handle).text()
}
)
});
assert_eq!(result, Some("Crate soup".to_string()));
Ok(())
}
but I'm confused about
(&span as &rcdom::Node).text();
(&span as &Handle).text()
trait NodeExt have text method,and struct Node and Handle implement it. but why can I convert reference of struct handle to other reference (handle and node) without compiler error? is it safe? I'm complete novice in rust.
pub trait NodeExt: Sized {
/// Retrieves the text value of this element, as well as it's child elements
fn text(&self) -> String {
let node = self.get_node();
let mut result = vec![];
extract_text(node, &mut result);
result.join("")
}
}
impl<'node> NodeExt for &'node rcdom::Node {
#[inline(always)]
fn get_node(&self) -> &rcdom::Node {
self
}
}
impl NodeExt for Handle {
#[inline(always)]
fn get_node(&self) -> &rcdom::Node {
&*self
}
}