Rust中如何从URL网址中提取主机等信息?

22-09-21 banq

从提供的 URL 字符串中提取主机和scheme等信息:

use url::{Url, Host, ParseError};

fn main() -> Result<(), ParseError> {

  let string = "https://jdon.com";
  let url = Url::parse(string)?;
  let scheme = url.scheme();
  let host = url.host();

  println!("Scheme: {}", scheme);
  println!("Host: {:?}", host);
   Ok(())

}