Skip to content

Commit 5e991e1

Browse files
committed
Improve AddrParseError documentation.
Add a potential cause to `AddrParseError` raising.
1 parent 1e1bfc7 commit 5e991e1

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

src/libstd/net/parser.rs

+19
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,25 @@ impl FromStr for SocketAddr {
372372
/// [`IpAddr`], [`Ipv4Addr`], [`Ipv6Addr`], [`SocketAddr`], [`SocketAddrV4`], and
373373
/// [`SocketAddrV6`].
374374
///
375+
/// # Potential causes
376+
///
377+
/// `AddrParseError` may be thrown because the provided string does not parse as the given type,
378+
/// often because it includes information only handled by a different address type.
379+
///
380+
/// ```should_panic
381+
/// use std::net::IpAddr;
382+
/// let _foo: IpAddr = "127.0.0.1:8080".parse().expect("Cannot handle the socket port");
383+
/// ```
384+
///
385+
/// [`IpAddr`] doesn't handle the port. Use [`SocketAddr`] instead.
386+
///
387+
/// ```
388+
/// use std::net::SocketAddr;
389+
///
390+
/// // No problem, the `panic!` message has disappeared.
391+
/// let _foo: SocketAddr = "127.0.0.1:8080".parse().expect("unreachable panic");
392+
/// ```
393+
///
375394
/// [`FromStr`]: ../../std/str/trait.FromStr.html
376395
/// [`IpAddr`]: ../../std/net/enum.IpAddr.html
377396
/// [`Ipv4Addr`]: ../../std/net/struct.Ipv4Addr.html

0 commit comments

Comments
 (0)