Rust: Parse a URL

Mar 25, 2022

Parsing a URL into its contituent parts is possible via url crate:

use url::Url;

fn main() {
    let url = "https://alice:secrets@glenngillen.com:8080/foo?q=bar#baz";
    let parts = Url::parse(url).expect("Unable to parse URL");
    
    println!("{}", parts.scheme());
    println!("{}", parts.username());
    println!("{}", parts.password().expect("no password"));
    println!("{}", parts.host_str().expect("no host"));
    println!("{}", parts.port().expect("no port"));
    println!("{}", parts.path());
    println!("{}", parts.query().expect("no query"));
    println!("{}", parts.fragment().expect("no fragment"));
}
Hi, I'm Glenn! 👋 I'm currently Director of Product (Terraform) @ HashiCorp, and we're hiring! If you'd like to come and work with me and help make Terraform Cloud even more amazing we have multiple positions opening in Product ManagementDesign, and Engineering & Engineering Management across a range of levels (i.e., junior through to senior). Please send in an application ASAP so we can get in touch.