url
import std::net::address::url; · source
Types
Url
type struct Url {
scheme: Str;
host: Str;
port: u16;
path: Str;
query: Option<Str>;
fragment: Option<Str>;
}
Url::parse reads absolute URLs — scheme://[user@]host[:port][/path][?query][#fragment] — into borrowed parts: every field is a slice of the source string, so parsing allocates nothing and the source has to outlive the Url. path is never empty ("/" when the URL had none); query and fragment come back without their leading ? and #. An IPv6 literal host [::1] is unwrapped to ::1, and any user@ userinfo is skipped. A missing port falls back to the scheme's well-known default — 80 for http and ws, 443 for https and wss, 21 for ftp — and to 0 for a scheme it does not know.
Three methods build text back from the parts. authority() is host[:port] as a Host header carries it, with an IPv6 host in brackets and the port only when it is not the scheme's default. path_and_query() is the target an HTTP request line names. join(reference) resolves a link or a Location header against this URL and returns the absolute URL it names; . and .. segments in a path it builds are removed.
import std::collections::str::{ Str };
import std::collections::string::{ String };
import std::net::address::url::{ Url };
const base: Url = Url::parse(Str::new("http://example.com/a/b?x=1")).unwrap();
const next: String = base.join(Str::new("../c")); // http://example.com/c
Parts come back verbatim otherwise — no %XX decoding. Display writes the URL back out. The HTTP client takes a Url for every request.
Properties
| Property | Type |
|---|---|
scheme | Str |
host | Str |
port | u16 |
path | Str |
query | Option<Str> |
fragment | Option<Str> |
Constructors
static parse(source: Str) -> Result<Url, UrlError>;
static parse(source: Str) -> Result<Url, UrlError>;Parse an absolute URL. See the module docs for the accepted shape.
Methods
authority(&this) -> String;
authority(&this) -> String;host[:port], the form a request's Host header carries: an IPv6 host in brackets, the port only when it is not the scheme's default.
path_and_query(&this) -> String;
path_and_query(&this) -> String;The path and, when there is one, ?query: the target an HTTP request line names. The fragment is never part of it.
join(&this, reference: Str) -> String;
join(&this, reference: Str) -> String;Resolve reference against this URL, as a link or a Location header is resolved against the page it came from, and return the absolute URL it names.
UrlError
type enum UrlError with [Eq, Clone, Debug] {
MissingScheme;
EmptyScheme;
EmptyHost;
InvalidPort;
}
The four ways a URL can fail to parse; message() is the human-readable form, and Display prints the same.
Variants
| Variant | Notes |
|---|---|
MissingScheme | No scheme:// - the input is not an absolute URL. |
EmptyScheme | A :// with nothing before it. |
EmptyHost | The authority had no host (scheme:///path, scheme://:80). |
InvalidPort | A :port that was empty, non-numeric, or out of u16 range. |
Methods
message(&this) -> Str;
message(&this) -> Str;A short human-readable reason, for diagnostics.
type struct Name { ... }
type enum Name { ... }
type trait Name { ... }
type union Name { ... }Begins a type declaration; the keyword that follows says which kind. On its own, type Name = Existing; declares an alias.
type struct Name {
field: Type;
method(&this) -> Ret { ... }
}A value type with named fields and optional methods. Structs live on the stack and are passed by value. Fields are public by default and may carry = default values; methods take their receiver as &this, mut &this, or this.
type struct Url {
scheme: Str;
host: Str;
port: u16;
path: Str;
query: Option<Str>;
fragment: Option<Str>;
}An absolute URL split into its parts, each a view into the text it was parsed from (keep that text alive while the Url is used). port is the scheme's default when the URL names none, and 0 for a scheme with no known default; path is / when the URL has none; query and fragment exclude their ? and #.
Url.scheme: Strtype struct Str {}A read-only view of UTF-8 text owned elsewhere: a string literal, a String, a file's bytes. Copying a Str copies the view, not the text, and it is valid only while what it views is. Str::new("text") views a literal; String.as_str() views a String.
Url.host: StrUrl.port: u16Url.path: StrUrl.query: Option<Str>type enum Option<T> with [Eq, Clone, Debug] {
Some(T);
None;
}A value of type T that may be absent: Some(value) or None.
Url.fragment: Option<Str>import a::b;
import a::b::{ X, Y };
import a::b as c;
import a::b::*;Brings a module, or selected items from it, into scope. One path per declaration: use the brace form for several items, as to alias, and * for everything public (prefer the brace form; wildcards invite name collisions).
type struct String<A = GlobalAllocator> {}Owned, growable UTF-8 text. It dereferences to Str, the read-only view most text functions take, so every Str method reads a String too (s.contains(...), s.split(...)); as_str hands the view out.
type struct Url {
scheme: Str;
host: Str;
port: u16;
path: Str;
query: Option<Str>;
fragment: Option<Str>;
}An absolute URL split into its parts, each a view into the text it was parsed from (keep that text alive while the Url is used). port is the scheme's default when the URL names none, and 0 for a scheme with no known default; path is / when the URL has none; query and fragment exclude their ? and #.
const NAME: Type = value;At module level, a true compile-time constant, conventionally SCREAMING_SNAKE_CASE. (mut at module level declares mutable global state instead.)
const base: Urlstatic Url::parse(source: Str) -> Result<Url, UrlError>Parse an absolute URL. See the module docs for the accepted shape.
std::net::address::urlstatic Str::new(text: string) -> StrConstruct from a NUL-terminated C-style string literal. The caller certifies that text is valid UTF-8 and outlives this view. The NUL terminator is not included in the view.
Result::unwrap(this) -> TThe Ok value, aborting on Err. Consumes the result: the value moves out to the caller and the error, if any, is released.
const next: Stringconst base: UrlUrl::join(&this, reference: Str) -> StringResolve reference against this URL, as a link or a Location header is resolved against the page it came from, and return the absolute URL it names.
type enum Name {
Unit,
Payload(T),
Named { x: i32 },
}An algebraic data type. Variants may be bare or carry a tuple or struct payload, and a match on an enum must cover every variant. Name::Variant names a variant; methods are added with implement enum Name { ... }.
type enum UrlError with [Eq, Clone, Debug] {
MissingScheme;
EmptyScheme;
EmptyHost;
InvalidPort;
}Why a URL failed to parse.
type struct Point with [Eq, Clone, Debug] { .. }Asks the compiler to generate implementations of the listed traits for the type, each working through its fields or variants in declaration order. The derivable traits are Eq, Ord, Hash, Clone, Debug, Default, ToValue and FromValue, and the text formats' FromJson, ToJson, FromYaml, ToYaml, FromToml and ToToml. The clause comes after the name and its parameters, before where.
type trait Eq<Rhs = This> {
equals(&this, other: &Rhs) -> boolean;
}Types that support value equality with a value of Rhs, which is the type itself unless an implementation names another: implement trait Eq for Str compares two Strs, and implement trait Eq<string> for Str a Str with a string.
type trait Clone {
clone(&this) -> This;
}A type whose values can be duplicated into independent copies. A type takes it with with [Clone] on its declaration, or by implementing clone by hand.
type trait Debug {
fmt(&this, f: mut &Formatter) -> Result<(), FormatError>;
}How a value reads to a programmer: what {x:?} writes, and {x:#?} over several lines. with [Debug] on a declaration writes it field by field.
UrlError::MissingSchemeNo scheme:// - the input is not an absolute URL.
UrlError::EmptySchemeA :// with nothing before it.
UrlError::EmptyHostThe authority had no host (scheme:///path, scheme://:80).
UrlError::InvalidPortA :port that was empty, non-numeric, or out of u16 range.
Url.path: StrUrl.query: Option<Str>Url.fragment: Option<Str>Url::authority(&this) -> Stringhost[:port], the form a request's Host header carries: an IPv6 host in brackets, the port only when it is not the scheme's default.
Url.host: StrUrl.port: u16Url::path_and_query(&this) -> StringThe path and, when there is one, ?query: the target an HTTP request line names. The fragment is never part of it.
type struct Location {}A place in the source: the file as the build was given it, the line and column (both from 1), and the readable qualified name of the function the place is in.
std::core::intrinsicstype trait Display {
fmt(&this, f: mut &Formatter) -> Result<(), FormatError>;
to_string(&this) -> String;
}How a value reads to a person: what {x} in an f-string and println write. Implement it for a type a user sees.
Url.scheme: Strstatic name(param: Type) -> Ret { ... }A method that belongs to the type rather than an instance, called as Type::name(..). For structs, static new(..) returning a struct literal is the idiomatic constructor.
(parameter) source: Strtype enum Result<T, E> with [Eq, Clone, Debug] {
Ok(T);
Err(E);
}The outcome of an operation that can fail: Ok(value) on success, Err(error) on failure.
type enum UrlError with [Eq, Clone, Debug] {
MissingScheme;
EmptyScheme;
EmptyHost;
InvalidPort;
}Why a URL failed to parse.
std::net::address::url&this
mut &this
thisThe receiver of a method. Its first parameter declares how the method takes it: &this borrows read-only, mut &this borrows exclusively, and this / mut this move the value in and consume it. In the body, this.field reaches the members.
(parameter) reference: StrUrlError::message(&this) -> StrA short human-readable reason, for diagnostics.
std::net::address::urlUrlError::InvalidPortA :port that was empty, non-numeric, or out of u16 range.