address
This library aids in processing network addresses. It adds the domain, endpoint, host, & authority
types that std lacks. The types have strict validation, owned & borrowed variants, and standard
library conversions.
Usage
= "0.22.0"
Features
This crate has no dependencies by default. The following optional features add dependencies:
idna: AddsDomain::parse_unicode&to_unicodefor international domain names.serde: Adds theSerialize&Deserializeimplementations for all address types.
Address Types
There are 6 core address types:
| Host | Without a port | With a port |
|---|---|---|
| IP Address | IPAddress |
SocketAddress |
| Domain | Domain & DomainRef |
Endpoint & EndpointRef |
| (Either) | Host & HostRef |
Authority & AuthorityRef |
Types that are not inherently Copy come in owned & reference pairs; the IP & socket address types
are Copy, so they have no reference form. The Ref types borrow their text, so they can parse &
convert without allocation.
Example
use ;
// Parsing validates and normalizes the address types.
let authority: Authority = "Example.com:443".parse.unwrap;
assert_eq!;
assert_eq!;
// You can easily convert between address types.
let authority: Authority = "127.0.0.1:80".parse.unwrap;
let socket: SocketAddress = authority.to_socket.unwrap;
assert_eq!;
// You can even convert to the addresses provided in the standard library.
use SocketAddr;
let socket: SocketAddr = socket.to_std;
assert_eq!;