url
import std::net::addr::url; · source
Types
Url
type struct Url {
scheme: Str;
host: Str;
port: u16;
path: Str;
query: Option<Str>;
fragment: Option<Str>;
static parse(source: Str) -> Result<Url, UrlError>;
static byte0(s: Str) -> u8;
static parse_port(digits: Str) -> Result<u16, UrlError>;
}
Url::parse reads absolute URLs — scheme://host:port/path?query#frag — into borrowed parts. path is never empty ("/" when the URL had none); query and fragment come back without their leading ? and #. A missing port falls back to the scheme's well-known default — 80 for http, 443 for https — and to 0 for a scheme it does not know.
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.
Static functions
static byte0(s: Str) -> u8;
static byte0(s: Str) -> u8;s's first byte (callers guard !s.is_empty() first).
static parse_port(digits: Str) -> Result<u16, UrlError>;
static parse_port(digits: Str) -> Result<u16, UrlError>;Parse a non-empty decimal port into a u16.
UrlError
type enum UrlError {
MissingScheme;
EmptyScheme;
EmptyHost;
InvalidPort;
}
implement enum UrlError {
message(&this) -> Str;
}
The four ways a URL can fail to parse; message() is the human-readable form.
It is a standalone parser. Parts come back verbatim — no %XX decoding, no ./.. collapsing — and http::Client still takes a SocketAddr plus a path, so wiring a Url-accepting client entry point is a remaining integration step.
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.
Constants
const COLON: u8 = 0x3A;
const SLASH: u8 = 0x2F;
const QUESTION: u8 = 0x3F;
const HASH: u8 = 0x23;
const AT: u8 = 0x40;
const LBRACKET: u8 = 0x5B;
const RBRACKET: u8 = 0x5D;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>;
}net::addr::url: a parser for absolute URLs.
Url.scheme: Strtype struct Str {
bytes: u8*;
length: u64;
}Str: a borrowed UTF-8 string slice.
std::collections::strUrl.host: StrUrl.port: u16Url.path: StrUrl.query: Option<Str>Url.fragment: Option<Str>static 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.
static Url::parse(source: Str) -> Result<Url, UrlError>Parse an absolute URL. See the module docs for the accepted shape.
(parameter) source: Strtype struct Url {
scheme: Str;
host: Str;
port: u16;
path: Str;
query: Option<Str>;
fragment: Option<Str>;
}net::addr::url: a parser for absolute URLs.
std::net::addr::urltype enum UrlError {
MissingScheme;
EmptyScheme;
EmptyHost;
InvalidPort;
}Why a URL failed to parse.
std::net::addr::urlstatic Url::byte0(s: Str) -> u8s's first byte (callers guard !s.is_empty() first).
(parameter) s: Strstatic Url::parse_port(digits: Str) -> Result<u16, UrlError>Parse a non-empty decimal port into a u16.
(parameter) digits: Strtype 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 {
MissingScheme;
EmptyScheme;
EmptyHost;
InvalidPort;
}Why a URL failed to parse.
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.
implement struct Name { ... }
implement trait Trait for Type { ... }
implement u64 { ... }Adds methods to a type outside its declaration. implement struct T (or enum, or a bare primitive) opens an inherent block; implement trait Tr for T supplies a trait's methods for one type. Resolution is at compile time - no dynamic dispatch.
implement enum Name { ... }Names the enum an inherent block extends - the only way to give an enum methods.
language referenceUrlError::message(&this) -> StrA short human-readable reason, for diagnostics.
&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.
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 COLON: u8 = 0x3A;const SLASH: u8 = 0x2F;const QUESTION: u8 = 0x3F;const HASH: u8 = 0x23;const AT: u8 = 0x40;const LBRACKET: u8 = 0x5B;const RBRACKET: u8 = 0x5D;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).
static Url::parse(source: Str) -> Result<Url, UrlError>Parse an absolute URL. See the module docs for the accepted shape.
std::net::addr::urlUrl.path: StrUrl.query: Option<Str>Url.fragment: Option<Str>Url.scheme: StrUrl.host: StrUrl.port: u16static Url::byte0(s: Str) -> u8s's first byte (callers guard !s.is_empty() first).
static Url::parse_port(digits: Str) -> Result<u16, UrlError>Parse a non-empty decimal port into a u16.