Skip to content

WIP: Update core::io and alloc::io Documentation - #160413

Closed
bushrat011899 wants to merge 5 commits into
rust-lang:mainfrom
bushrat011899:io_documentation
Closed

WIP: Update core::io and alloc::io Documentation#160413
bushrat011899 wants to merge 5 commits into
rust-lang:mainfrom
bushrat011899:io_documentation

Conversation

@bushrat011899

@bushrat011899 bushrat011899 commented Aug 3, 2026

Copy link
Copy Markdown
Contributor

ACP: rust-lang/libs-team#755
Tracking issue: #154046

Description

Now that core::io and alloc::io contain a significant proportion of formerly std-only code, there's a lot of documentation that requires updating to ensure no_std users are provided relevant information. For example, a lot of demonstration code relies on File, which is not available in alloc or core. Further, this comment on #156527, and #160151 have both identified similar issues.

Solution

  • Moved BufWriter's TcpStream example code to the TcpStream type. The example is useful and worth preserving, but would need to be completely replaced if left in alloc.
  • Updated many examples across core::io and alloc::io to use core and alloc compatible types where they previously relied on stdin, File, TcpStream, etc.
    • Notably, I've used hidden use core as std; (and similar) statements to ensure the live documentation on docs.rs shows stable paths, since these types are re-exported from std.

Notes

  • No AI tooling of any kind was used during the creation of this PR.
  • Creating as draft as I suspect this will require a few iterations to pass CI.

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Aug 3, 2026
Comment thread library/alloc/src/io/buffered/bufreader.rs Outdated
Comment on lines -489 to -518
///
/// # Example
///
/// ```
/// # // This test requires unwinding to work.
/// # // Disable it when unwinding isn't available.
/// # #[cfg(panic = "unwind")]
/// # fn main() {
/// use std::io::{self, BufWriter, Write};
/// use std::panic::{catch_unwind, AssertUnwindSafe};
///
/// struct PanickingWriter;
/// impl Write for PanickingWriter {
/// fn write(&mut self, buf: &[u8]) -> io::Result<usize> { panic!() }
/// fn flush(&mut self) -> io::Result<()> { panic!() }
/// }
///
/// let mut stream = BufWriter::new(PanickingWriter);
/// write!(stream, "some data").unwrap();
/// let result = catch_unwind(AssertUnwindSafe(|| {
/// stream.flush().unwrap()
/// }));
/// assert!(result.is_err());
/// let (recovered_writer, buffered_data) = stream.into_parts();
/// assert!(matches!(recovered_writer, PanickingWriter));
/// assert_eq!(buffered_data.unwrap_err().into_inner(), b"some data");
/// # }
/// # #[cfg(not(panic = "unwind"))]
/// # fn main() {}
/// ```

@bushrat011899 bushrat011899 Aug 3, 2026

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.

Since this example relies on catch_unwind, it requires std. I've removed it temporarily, but maybe this needs to be added back to WriterPanicked (maybe on the use alloc_crate::io::WriterPanicked re-export in std?)

View changes since the review

Comment thread library/alloc/src/io/buffered/linewriter.rs Outdated
Comment thread library/alloc/src/io/buf_read.rs
Comment thread library/alloc/src/io/mod.rs Outdated
Comment on lines -742 to -753
/// # Examples
///
/// ```no_run
/// # use std::io;
/// fn main() -> io::Result<()> {
/// let stdin = io::read_to_string(io::stdin())?;
/// println!("Stdin was:");
/// println!("{stdin}");
/// Ok(())
/// }
/// ```
///

@bushrat011899 bushrat011899 Aug 3, 2026

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.

Not sure what to do about this example; I think without stdin this example would be too small to really be worth writing?

View changes since the review

Comment thread library/core/src/io/cursor.rs Outdated
@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

///
/// fn main() -> std::io::Result<()> {
/// let f1 = File::open("log.txt")?;
/// let f1 = b"Hello\nWorld!" as &[u8];

@bjorn3 bjorn3 Aug 3, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think it is reasonable to keep the old code. It better explains what kind of use cases BufReader is meant for (reads involving actual IO that is cheaper when batched).

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.

I do think the old examples are generally better, but I'm not sure if there's any issues with having examples in alloc which require std? If it's acceptable to have example code in alloc and core which requires std I'm happy to revert those changes.

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.

Actually yeah, based on the existing documentation for something like core::borrow, it looks like it's expected that std be available for example snippets. Tomorrow morning I'll close this PR in favor of just the prelude and a handful of other minor changes. Thanks for your help!

@bushrat011899

Copy link
Copy Markdown
Contributor Author

Closing in favour of #160472. I chose to open a fresh PR and leave this closed so that if someone did want to make the snippets no_std/no_alloc in the future, they have a hint as to the amount of changes that entails.

@rustbot rustbot removed the S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. label Aug 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

T-libs Relevant to the library team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants