Skip to content
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions library/alloc/src/io/buffered/bufwriter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ use crate::vec::Vec;
/// use std::io::prelude::*;
/// use std::net::TcpStream;
///
/// let mut stream = TcpStream::connect("127.0.0.1:34254").unwrap();
/// let mut stream = TcpStream::connect("[::1]:34254").unwrap();

@NobodyXu NobodyXu Aug 30, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about keeping both versions side-by-side?

Many, for example me, aren't familiar with IPv6, having both will help me read the docs while bridge me into ipv6

Suggested change
/// let mut stream = TcpStream::connect("[::1]:34254").unwrap();
/// let mut stream = TcpStream::connect("127.0.0.1:34254").unwrap(); // ipv4
/// let mut stream = TcpStream::connect("[::1]:34254").unwrap(); // ipv6

View changes since the review

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would this be for every TcpStream::connect in the docs?
It's just the localhost address everywhere, but if the duplication is not a problem, sure! 👍🏻

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this idea, honestly. Since examples are also doctests, it makes sense to have "coverage" for these, I think.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would I just redeclare the stream/whateverit'snamed variable on every doctest or how would we do that? :-)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean, it would depend on the test, but you're welcome to use your own judgement. It would be fine to define both but only "use" the v6 one imo

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

considering that adding both versions to every site where I changed it would be a lot of clutter, I suggest my newest change, which is adding an ipv4_x declaration to UdpSocket, TcpListener and TcpStream struct examples (meaning they show up on the top of the page in rustdoc)

///
/// for i in 0..10 {
/// stream.write(&[i+1]).unwrap();
Expand All @@ -50,7 +50,7 @@ use crate::vec::Vec;
/// use std::io::BufWriter;
/// use std::net::TcpStream;
///
/// let mut stream = BufWriter::new(TcpStream::connect("127.0.0.1:34254").unwrap());
/// let mut stream = BufWriter::new(TcpStream::connect("[::1]:34254").unwrap());
///
/// for i in 0..10 {
/// stream.write(&[i+1]).unwrap();
Expand Down Expand Up @@ -91,7 +91,7 @@ impl<W: Write> BufWriter<W> {
/// use std::net::TcpStream;
///
/// # #[expect(unused_mut)]
/// let mut buffer = BufWriter::new(TcpStream::connect("127.0.0.1:34254").unwrap());
/// let mut buffer = BufWriter::new(TcpStream::connect("[::1]:34254").unwrap());
/// ```
#[cfg(not(no_global_oom_handling))]
#[stable(feature = "rust1", since = "1.0.0")]
Expand Down Expand Up @@ -122,7 +122,7 @@ impl<W: Write> BufWriter<W> {
/// use std::io::BufWriter;
/// use std::net::TcpStream;
///
/// let stream = TcpStream::connect("127.0.0.1:34254").unwrap();
/// let stream = TcpStream::connect("[::1]:34254").unwrap();
/// # #[expect(unused_mut)]
/// let mut buffer = BufWriter::with_capacity(100, stream);
/// ```
Expand All @@ -147,7 +147,7 @@ impl<W: Write> BufWriter<W> {
/// use std::net::TcpStream;
///
/// # #[expect(unused_mut)]
/// let mut buffer = BufWriter::new(TcpStream::connect("127.0.0.1:34254").unwrap());
/// let mut buffer = BufWriter::new(TcpStream::connect("[::1]:34254").unwrap());
///
/// // unwrap the TcpStream and flush the buffer
/// let stream = buffer.into_inner().unwrap();
Expand Down Expand Up @@ -301,7 +301,7 @@ impl<W: ?Sized + Write> BufWriter<W> {
/// use std::net::TcpStream;
///
/// # #[expect(unused_mut)]
/// let mut buffer = BufWriter::new(TcpStream::connect("127.0.0.1:34254").unwrap());
/// let mut buffer = BufWriter::new(TcpStream::connect("[::1]:34254").unwrap());
///
/// // we can use reference just like buffer
/// let reference = buffer.get_ref();
Expand All @@ -321,7 +321,7 @@ impl<W: ?Sized + Write> BufWriter<W> {
/// use std::io::BufWriter;
/// use std::net::TcpStream;
///
/// let mut buffer = BufWriter::new(TcpStream::connect("127.0.0.1:34254").unwrap());
/// let mut buffer = BufWriter::new(TcpStream::connect("[::1]:34254").unwrap());
///
/// // we can use reference just like buffer
/// let reference = buffer.get_mut();
Expand All @@ -339,7 +339,7 @@ impl<W: ?Sized + Write> BufWriter<W> {
/// use std::io::BufWriter;
/// use std::net::TcpStream;
///
/// let buf_writer = BufWriter::new(TcpStream::connect("127.0.0.1:34254").unwrap());
/// let buf_writer = BufWriter::new(TcpStream::connect("[::1]:34254").unwrap());
///
/// // See how many bytes are currently buffered
/// let bytes_buffered = buf_writer.buffer().len();
Expand Down Expand Up @@ -371,7 +371,7 @@ impl<W: ?Sized + Write> BufWriter<W> {
/// use std::io::BufWriter;
/// use std::net::TcpStream;
///
/// let buf_writer = BufWriter::new(TcpStream::connect("127.0.0.1:34254").unwrap());
/// let buf_writer = BufWriter::new(TcpStream::connect("[::1]:34254").unwrap());
///
/// // Check the capacity of the inner buffer
/// let capacity = buf_writer.capacity();
Expand Down
6 changes: 3 additions & 3 deletions library/alloc/src/io/buffered/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use crate::io::Error;
/// use std::net::TcpStream;
///
/// # #[expect(unused_mut)]
/// let mut stream = BufWriter::new(TcpStream::connect("127.0.0.1:34254").unwrap());
/// let mut stream = BufWriter::new(TcpStream::connect("[::1]:34254").unwrap());
///
/// // do stuff with the stream
///
Expand Down Expand Up @@ -68,7 +68,7 @@ impl<W> IntoInnerError<W> {
/// use std::net::TcpStream;
///
/// # #[expect(unused_mut)]
/// let mut stream = BufWriter::new(TcpStream::connect("127.0.0.1:34254").unwrap());
/// let mut stream = BufWriter::new(TcpStream::connect("[::1]:34254").unwrap());
///
/// // do stuff with the stream
///
Expand Down Expand Up @@ -103,7 +103,7 @@ impl<W> IntoInnerError<W> {
/// use std::net::TcpStream;
///
/// # #[expect(unused_mut)]
/// let mut stream = BufWriter::new(TcpStream::connect("127.0.0.1:34254").unwrap());
/// let mut stream = BufWriter::new(TcpStream::connect("[::1]:34254").unwrap());
///
/// // do stuff with the stream
///
Expand Down
2 changes: 1 addition & 1 deletion library/core/src/error.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ Of the two, `expect` is generally preferred since its `msg` field allows you
to convey your intent and assumptions which makes tracking down the source
of a panic easier. `unwrap` on the other hand can still be a good fit in
situations where you can trivially show that a piece of code will never
panic, such as `"127.0.0.1".parse::<std::net::IpAddr>().unwrap()` or early
panic, such as `"[::1]".parse::<std::net::IpAddr>().unwrap()` or early
prototyping.

# Common Message Styles
Expand Down
8 changes: 4 additions & 4 deletions library/core/src/fmt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2417,12 +2417,12 @@ impl<'a> Formatter<'a> {
///
/// ```rust
/// use std::fmt;
/// use std::net::Ipv4Addr;
/// use std::net::Ipv6Addr;
///
/// struct Foo {
/// bar: i32,
/// baz: String,
/// addr: Ipv4Addr,
/// addr: Ipv6Addr,
/// }
///
/// impl fmt::Debug for Foo {
Expand All @@ -2436,11 +2436,11 @@ impl<'a> Formatter<'a> {
/// }
///
/// assert_eq!(
/// "Foo { bar: 10, baz: \"Hello World\", addr: 127.0.0.1 }",
/// "Foo { bar: 10, baz: \"Hello World\", addr: ::1 }",
/// format!("{:?}", Foo {
/// bar: 10,
/// baz: "Hello World".to_string(),
/// addr: Ipv4Addr::new(127, 0, 0, 1),
/// addr: Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1),
/// })
/// );
/// ```
Expand Down
4 changes: 2 additions & 2 deletions library/core/src/net/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,7 @@ enum AddrKind {
///
/// ```should_panic
/// use std::net::IpAddr;
/// let _foo: IpAddr = "127.0.0.1:8080".parse().expect("Cannot handle the socket port");
/// let _foo: IpAddr = "[::1]:8080".parse().expect("Cannot handle the socket port");
/// ```
///
/// [`IpAddr`] doesn't handle the port. Use [`SocketAddr`] instead.
Expand All @@ -480,7 +480,7 @@ enum AddrKind {
/// use std::net::SocketAddr;
///
/// // No problem, the `panic!` message has disappeared.
/// let _foo: SocketAddr = "127.0.0.1:8080".parse().expect("`parse` should succeed");
/// let _foo: SocketAddr = "[::1]:8080".parse().expect("`parse` should succeed");
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
#[derive(Debug, Clone, PartialEq, Eq)]
Expand Down
8 changes: 4 additions & 4 deletions library/core/src/net/socket_addr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ use crate::net::{IpAddr, Ipv4Addr, Ipv6Addr};
/// # Examples
///
/// ```
/// use std::net::{IpAddr, Ipv4Addr, SocketAddr};
/// use std::net::{IpAddr, Ipv6Addr, SocketAddr};
///
/// let socket = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 8080);
/// let socket = SocketAddr::new(IpAddr::V6(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1)), 8080);
///
/// assert_eq!("127.0.0.1:8080".parse(), Ok(socket));
/// assert_eq!("[::1]:8080".parse(), Ok(socket));
/// assert_eq!(socket.port(), 8080);
/// assert_eq!(socket.is_ipv4(), true);
/// assert_eq!(socket.is_ipv6(), true);
/// ```
#[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
#[stable(feature = "rust1", since = "1.0.0")]
Expand Down
18 changes: 9 additions & 9 deletions library/std/src/net/socket_addr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,9 @@ use crate::{io, iter, option, slice, vec};
/// ```no_run
/// use std::net::{SocketAddr, ToSocketAddrs};
///
/// // assuming 'localhost' resolves to 127.0.0.1
/// // assuming 'localhost' resolves to [::1]
/// let mut addrs_iter = "localhost:443".to_socket_addrs().unwrap();
/// assert_eq!(addrs_iter.next(), Some(SocketAddr::from(([127, 0, 0, 1], 443))));
/// assert_eq!(addrs_iter.next(), Some(SocketAddr::from(([0, 0, 0, 0, 0, 0, 0, 1], 443))));
/// assert!(addrs_iter.next().is_none());
///
/// // assuming 'foo' does not resolve
Expand All @@ -87,8 +87,8 @@ use crate::{io, iter, option, slice, vec};
/// ```
/// use std::net::{SocketAddr, ToSocketAddrs};
///
/// let addr1 = SocketAddr::from(([0, 0, 0, 0], 80));
/// let addr2 = SocketAddr::from(([127, 0, 0, 1], 443));
/// let addr1 = SocketAddr::from(([0x2001, 0xdb8, 0, 0, 0, 0, 0, 1], 80));
Comment thread
2ndDerivative marked this conversation as resolved.
/// let addr2 = SocketAddr::from(([0, 0, 0, 0, 0, 0, 0, 1], 443));
/// let addrs = vec![addr1, addr2];
///
/// let mut addrs_iter = (&addrs[..]).to_socket_addrs().unwrap();
Expand All @@ -105,7 +105,7 @@ use crate::{io, iter, option, slice, vec};
/// use std::io;
/// use std::net::ToSocketAddrs;
///
/// let err = "127.0.0.1".to_socket_addrs().unwrap_err();
/// let err = "[::1]".to_socket_addrs().unwrap_err();
/// assert_eq!(err.kind(), io::ErrorKind::InvalidInput);
/// ```
///
Expand All @@ -114,13 +114,13 @@ use crate::{io, iter, option, slice, vec};
/// different types:
///
/// ```no_run
/// use std::net::{TcpStream, Ipv4Addr};
/// use std::net::{TcpStream, Ipv6Addr};
///
/// let stream = TcpStream::connect(("127.0.0.1", 443));
/// let stream = TcpStream::connect(("[::1]", 443));
/// // or
/// let stream = TcpStream::connect("127.0.0.1:443");
/// let stream = TcpStream::connect("[::1]:443");
/// // or
/// let stream = TcpStream::connect((Ipv4Addr::new(127, 0, 0, 1), 443));
/// let stream = TcpStream::connect((Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1), 443));
/// ```
///
/// [`TcpStream::connect`]: crate::net::TcpStream::connect
Expand Down
Loading
Loading