Skip to content

Latest commit

 

History

History
33 lines (21 loc) · 782 Bytes

new.zh.md

File metadata and controls

33 lines (21 loc) · 782 Bytes

从一个 base URL ,创建新的 URL

[![url-badge]][url] [![cat-net-badge]][cat-net]

这个join方法,用 base 路径和相对路径,创建新的 URL。

extern crate url;

use url::{Url, ParseError};

fn main() -> Result<(), ParseError> {
    let path = "/rust-lang/cargo";

    let gh = build_github_url(path)?;

    assert_eq!(gh.as_str(), "https://github.com/rust-lang/cargo");
    println!("The joined URL is: {}", gh);

    Ok(())
}

fn build_github_url(path: &str) -> Result<Url, ParseError> {
    const GITHUB: &'static str = "https://github.com";

    let base = Url::parse(GITHUB).expect("hardcoded URL is known to be valid");
    let joined = base.join(path)?;

    Ok(joined)
}