Skip to main content

Module host

Module host 

Source
Available on crate feature net only.
Expand description

Host type for network programming.

This module provides a unified Host enum that can represent IP addresses, domain names, or hostnames with automatic parsing.

§Parsing Order

When parsing a string, the Host type attempts to parse in this order:

  1. IpAddr - IPv4 or IPv6 addresses (e.g., “192.168.1.1”, “::1”)
  2. DomainName - RFC 1035 domain names (e.g., “example.com”)
  3. Hostname - RFC 1123 hostnames (e.g., “localhost”)

§Examples

use bare_types::net::Host;

// Parse an IP address
let host: Host = "192.168.1.1".parse().unwrap();
assert!(host.is_ipaddr());

// Parse a domain name (labels can start with digits)
let host: Host = "123.example.com".parse().unwrap();
assert!(host.is_domainname());

// Create a hostname directly (labels must start with letters)
let hostname = "localhost".parse::<bare_types::net::Hostname>().unwrap();
let host = Host::from_hostname(hostname);
assert!(host.is_hostname());

Enums§

Host
A network host that can be an IP address, domain name, or hostname.
HostError
Error type for host parsing.