Url
A parsed URL: scheme://userinfo@host:port/path?query#fragment.
The parts are kept as written (not percent-decoded), except that the scheme and the host are
lowercased, which URLs treat as case-insensitive. Any part but the scheme and the path may be
absent. Display writes the URL back, so a normalized URL round-trips. This is a general
RFC 3986 parser, not the WHATWG one: it does not know special schemes, punycode, or percent-
encode for you (see percent_encode), and it rejects spaces and
control characters instead of fixing them.
Import
Section titled “Import”Cargo feature url (enabled by default). To compile only this module:
or in Cargo.toml:
Definition
Section titled “Definition”Examples
Section titled “Examples”Methods
Section titled “Methods”Parses an absolute URL.
Parameters
| Parameter | Type | Description |
|---|---|---|
input | &str | The URL, which must start with a scheme (https:, mailto:, file:, …). |
Returns
Result<Self, UrlError>
Errors
A UrlError for a space or control character, a missing or invalid scheme, a malformed
host, or a port that is not a number from 0 to 65535.
Resolves reference against this URL, as a browser resolves a link on a page (RFC 3986,
section 5).
The reference may be absolute (https://other.example/), start with // (keep the
scheme), a path (/a, b, ../c), only a query (?x=1) or only a fragment (#top).
. and .. segments are resolved.
Parameters
| Parameter | Type | Description |
|---|---|---|
reference | &str | The URL reference to resolve. |
Returns
Result<Self, UrlError>
Errors
The same UrlErrors as Url::parse, for the reference.
scheme
Section titled “scheme”The scheme, lowercase, such as "https".
Returns
&str — The scheme without the colon.
userinfo
Section titled “userinfo”The user information before the @, such as "user:password".
Returns
Option<&str> — The raw user information, or None when the URL has none.
The host, lowercase, with the brackets of an IPv6 literal ("[::1]").
Returns
Option<&str> — The host, Some("") for an empty authority (file:///etc), or None when the URL has no
authority at all (mailto:a@b.c).
The port written in the URL.
Returns
Option<u16> — The port, or None when the URL does not name one.
port_or_default
Section titled “port_or_default”The port to connect to: the one in the URL, or the default of a well-known scheme.
Returns
Option<u16> — The explicit port, else 80 for http and ws, 443 for https and wss, 21 for
ftp, and None for any other scheme.
The path, as written; possibly empty.
Returns
&str — The path, starting with / when the URL has an authority and a path.
The query, without the ?.
Returns
Option<&str> — The raw query (see parse_query to decode it), or None.
fragment
Section titled “fragment”The fragment, without the #.
Returns
Option<&str> — The raw fragment, or None.
Error type: UrlError
Section titled “Error type: UrlError”Why a string could not be parsed as a URL.
