Enum SocketAddr
pub enum SocketAddr
An internet socket address, either IPv4 or IPv6.
Internet socket addresses consist of an IP address, a 16-bit port number, as well
as possibly some version-dependent additional information. See SocketAddrV4's and
SocketAddrV6's respective documentation for more details.
Portability
SocketAddr is intended to be a portable representation of socket addresses and is likely not
the same as the internal socket address type used by the target operating system's API. Like all
repr(Rust) structs, however, its exact layout remains undefined and should not be relied upon
between builds.
Examples
use ;
let socket = new;
assert_eq!;
assert_eq!;
assert_eq!;
Variants
-
V4(SocketAddrV4) An IPv4 socket address.
-
V6(SocketAddrV6) An IPv6 socket address.
Implementations
impl SocketAddr
fn parse_ascii(b: &[u8]) -> Result<Self, AddrParseError>Parse a socket address from a slice of bytes.
use ; let socket_v4 = new; let socket_v6 = new; assert_eq!; assert_eq!;
impl SocketAddr
const fn new(ip: IpAddr, port: u16) -> SocketAddrCreates a new socket address from an IP address and a port number.
Examples
use ; let socket = new; assert_eq!; assert_eq!;const fn ip(&self) -> IpAddrReturns the IP address associated with this socket address.
Examples
use ; let socket = new; assert_eq!;const fn set_ip(&mut self, new_ip: IpAddr)Changes the IP address associated with this socket address.
Examples
use ; let mut socket = new; socket.set_ip; assert_eq!;const fn port(&self) -> u16Returns the port number associated with this socket address.
Examples
use ; let socket = new; assert_eq!;const fn set_port(&mut self, new_port: u16)Changes the port number associated with this socket address.
Examples
use ; let mut socket = new; socket.set_port; assert_eq!;const fn is_ipv4(&self) -> boolReturns
trueif the IP address in thisSocketAddris anIPv4address, andfalseotherwise.Examples
use ; let socket = new; assert_eq!; assert_eq!;const fn is_ipv6(&self) -> boolReturns
trueif the IP address in thisSocketAddris anIPv6address, andfalseotherwise.Examples
use ; let socket = new; assert_eq!; assert_eq!;const fn unspecified_from(this: Self) -> SelfReturns the unspecified socket address for the same IP version.
Returns
0.0.0.0:0for IPv4 and[::]:0for IPv6. For IPv6, this method preserves the flow information and scope ID.Use this method when you must bind a socket to an unspecified local address that uses the same IP version as a remote address.
Examples
use ; let addr = V4; assert_eq!; let addr = V6; let unspecified = unspecified_from; assert_eq!; if let V6 = unspecified
Trait Implementations
impl Clone for SocketAddr
fn clone(&self) -> SocketAddr
impl Copy for SocketAddr
impl Debug for SocketAddr
fn fmt(&self, fmt: &mut Formatter<'_>) -> Result
impl Display for SocketAddr
fn fmt(&self, f: &mut Formatter<'_>) -> Result
impl Eq for SocketAddr
fn assert_fields_are_eq(&self)
impl From<SocketAddrV4> for SocketAddr
fn from(sock4: SocketAddrV4) -> SocketAddrConverts a
SocketAddrV4into aSocketAddr::V4.
impl From<SocketAddrV6> for SocketAddr
fn from(sock6: SocketAddrV6) -> SocketAddrConverts a
SocketAddrV6into aSocketAddr::V6.
impl FromStr for SocketAddr
type Err = AddrParseError;fn from_str(s: &str) -> Result<SocketAddr, AddrParseError>
impl Hash for SocketAddr
fn hash<__H: Hasher>(&self, state: &mut __H)
impl Ord for SocketAddr
fn cmp(&self, other: &SocketAddr) -> Ordering
impl PartialEq for SocketAddr
fn eq(&self, other: &SocketAddr) -> bool
impl PartialOrd for SocketAddr
fn partial_cmp(&self, other: &SocketAddr) -> Option<Ordering>
impl StructuralPartialEq for SocketAddr
impl TrivialClone for SocketAddr
impl<I: ~const Into<IpAddr>> From<(I, u16)> for SocketAddr
fn from(pieces: (I, u16)) -> SocketAddrConverts a tuple struct (Into<
IpAddr>,u16) into aSocketAddr.This conversion creates a
SocketAddr::V4for anIpAddr::V4and creates aSocketAddr::V6for anIpAddr::V6.u16is treated as port of the newly createdSocketAddr.
Auto Trait Implementations
impl Freeze for SocketAddr
impl RefUnwindSafe for SocketAddr
impl Send for SocketAddr
impl Sync for SocketAddr
impl Unpin for SocketAddr
impl UnsafeUnpin for SocketAddr
impl UnwindSafe for SocketAddr
Blanket Implementations
impl<T> Any for SocketAddr
where
T: 'static + ?Sized,
fn type_id(&self) -> TypeId
impl<T> Borrow<T> for SocketAddr
where
T: ?Sized,
fn borrow(&self) -> &T
impl<T> BorrowMut<T> for SocketAddr
where
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
impl<T> CloneToUninit for SocketAddr
where
T: Clone,
unsafe fn clone_to_uninit(&self, dest: *mut u8)
impl<T> From<T> for SocketAddr
fn from(t: T) -> TReturns the argument unchanged.
impl<T> Printable for SocketAddr
where
T: Copy + Debug,
impl<T> SizeHint for SocketAddr
where
T: ?Sized,
fn lower_bound(&self) -> usizefn upper_bound(&self) -> Option<usize>
impl<T> SizedTypeProperties for SocketAddr
impl<T, U> Into<U> for SocketAddr
where
U: From<T>,
fn into(self) -> UCalls
U::from(self).That is, this conversion is whatever the implementation of
[From]<T> for Uchooses to do.
impl<T, U> TryFrom<U> for SocketAddr
where
U: Into<T>,
type Error = Infallible;fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
impl<T, U> TryInto<U> for SocketAddr
where
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error;fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>