pub struct Url<'url> { /* private fields */ }
Expand description
A parsed URL with support for hostnames, IPv4/IPv6 addresses, userinfo, ports, paths, queries, and fragments.
This struct represents a URL parsed from a string, with all components accessible individually. It supports both ASCII and UTF-8 characters in all components, and properly handles subdomains, custom TLDs, and internationalized domain names (IDNs).
§Examples
use faup_rs::Url;
// Parse a simple URL
let url = Url::parse("https://example.com").unwrap();
assert_eq!(url.scheme(), "https");
assert_eq!(url.host().as_hostname().unwrap().full_name(), "example.com");
// Parse a URL with all components
let url = Url::parse("https://user:pass@sub.example.com:8080/path?query=value#fragment").unwrap();
assert_eq!(url.scheme(), "https");
assert_eq!(url.userinfo().unwrap().username(), "user");
assert_eq!(url.port(), Some(8080));
assert_eq!(url.path(), Some("/path"));
assert_eq!(url.query(), Some("query=value"));
assert_eq!(url.fragment(), Some("fragment"));
Implementations§
Source§impl<'url> Url<'url>
impl<'url> Url<'url>
Sourcepub fn into_owned<'owned>(self) -> Url<'owned>
pub fn into_owned<'owned>(self) -> Url<'owned>
Converts this borrowed Url
into an owned Url
.
This is useful when you need to store the Url
for longer than the lifetime of the input string.
§Performance
When using this method strings will be cloned.
§Returns
Url<'owned>
- An owned version of the URL.
§Examples
use faup_rs::Url;
let url = Url::parse("https://example.com").unwrap();
let owned_url = url.into_owned();
Sourcepub fn parse(s: &'url str) -> Result<Self, Error>
pub fn parse(s: &'url str) -> Result<Self, Error>
Creates a new Url
by parsing a string slice.
§Arguments
s
- A string slice containing the URL to parse.
§Returns
Result<Url, Error>
- A parsedUrl
if successful, or anError
if parsing fails.
§Examples
use faup_rs::Url;
let url = Url::parse("https://example.com").unwrap();
assert_eq!(url.scheme(), "https");
assert_eq!(url.domain(), Some("example.com"));
assert_eq!(url.suffix(), Some("com"));
Sourcepub fn userinfo(&self) -> Option<&UserInfo<'_>>
pub fn userinfo(&self) -> Option<&UserInfo<'_>>
Returns the user information component of the URL, if present.
§Returns
Option<&UserInfo>
- The user information, orNone
if not present.
§Examples
use faup_rs::Url;
let url = Url::parse("https://user:pass@example.com").unwrap();
assert_eq!(url.userinfo().unwrap().username(), "user");
assert_eq!(url.userinfo().unwrap().password(), Some("pass"));
Sourcepub fn host(&self) -> &Host<'_>
pub fn host(&self) -> &Host<'_>
Returns the host component of the URL.
§Returns
&Host
- The host, which can be either a hostname or an IP address.
§Examples
use faup_rs::Url;
let url = Url::parse("https://sub2.sub1.example.com").unwrap();
let hostname = url.host().as_hostname().unwrap();
assert_eq!(hostname.full_name(), "sub2.sub1.example.com");
assert_eq!(hostname.domain(), Some("example.com"));
assert_eq!(hostname.suffix(), Some("com"));
assert_eq!(hostname.subdomain(), Some("sub2.sub1"));
Sourcepub fn domain(&self) -> Option<&str>
pub fn domain(&self) -> Option<&str>
Returns the domain part of the hostname, if present.
This is a convenience method that directly accesses the domain component of the hostname, if the host is a hostname (not an IP address).
§Returns
Option<&str>
- The domain part of the hostname, orNone
if:- The host is an IP address
- The hostname doesn’t have a recognized domain
§Examples
use faup_rs::Url;
// With a domain name
let url = Url::parse("https://sub.example.com").unwrap();
assert_eq!(url.domain(), Some("example.com"));
// With an IP address
let url = Url::parse("https://127.0.0.1").unwrap();
assert_eq!(url.domain(), None);
Sourcepub fn subdomain(&self) -> Option<&str>
pub fn subdomain(&self) -> Option<&str>
Returns the subdomain part of the hostname, if present.
This is a convenience method that directly accesses the subdomain component of the hostname, if the host is a hostname (not an IP address).
§Returns
Option<&str>
- The subdomain part of the hostname, orNone
if:- The host is an IP address
- The hostname doesn’t have a subdomain
§Examples
use faup_rs::Url;
// With a subdomain
let url = Url::parse("https://sub.example.com").unwrap();
assert_eq!(url.subdomain(), Some("sub"));
// Without a subdomain
let url = Url::parse("https://example.com").unwrap();
assert_eq!(url.subdomain(), None);
// With an IP address
let url = Url::parse("https://127.0.0.1").unwrap();
assert_eq!(url.subdomain(), None);
Sourcepub fn suffix(&self) -> Option<&str>
pub fn suffix(&self) -> Option<&str>
Returns the suffix (top-level domain) of the hostname, if present.
This is a convenience method that directly accesses the suffix component of the hostname, if the host is a hostname (not an IP address).
§Returns
Option<&str>
- The suffix (TLD) of the hostname, orNone
if:- The host is an IP address
- The hostname doesn’t have a recognized suffix
§Examples
use faup_rs::Url;
// With a standard TLD
let url = Url::parse("https://example.com").unwrap();
assert_eq!(url.suffix(), Some("com"));
// With a custom TLD
let url = Url::parse("http://example.b32.i2p").unwrap();
assert_eq!(url.suffix(), Some("b32.i2p"));
// With an IP address
let url = Url::parse("https://127.0.0.1").unwrap();
assert_eq!(url.suffix(), None);