Add the url crate to Cargo.toml and use Url::parse() to extract scheme, host, and path from a string.
Add the url crate to your Cargo.toml and parse strings using Url::parse().
[dependencies]
url = "2.5"
use url::Url;
fn main() {
let url = Url::parse("https://example.com/path?query=1").unwrap();
println!("Scheme: {}", url.scheme());
println!("Host: {}", url.host_str().unwrap());
println!("Path: {}", url.path());
}
The url crate is a tool that breaks web addresses into their individual parts like the domain, path, and search terms. You use it whenever your program needs to read or modify a website link safely. Think of it like a map that shows you exactly where every piece of an address belongs.