pub fn parse_ipv6_literal(s: &str) -> Option<[u8; 16]>Expand description
Parses an IPv6 literal into network-order octets.
Supports standard colon-hex notation (RFC 4291) with :: compression.
§Limitations
- IPv4-mapped/embedded forms (e.g.
::ffff:192.168.1.1) are not supported; only pure colon-hex notation is accepted. - Zone IDs (e.g.
fe80::1%eth0) are not supported; the zone identifier must be stripped before calling this function.
§Example
ⓘ
assert_eq!(
parse_ipv6_literal("2001:0db8:0000:0000:0000:ff00:0042:8329"),
Some([
0x20, 0x01, 0x0d, 0xb8, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0xff, 0x00, 0x00, 0x42, 0x83, 0x29,
])
);
// Compressed forms
assert_eq!(
parse_ipv6_literal("::1"),
Some([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1])
);
assert_eq!(
parse_ipv6_literal("fe80::5054:ff:fe12:3456"),
Some([
0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x50, 0x54, 0x00, 0xff, 0xfe, 0x12, 0x34, 0x56,
])
);
// Loopback
assert_eq!(
parse_ipv6_literal("::1"),
Some([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1])
);