Skip to content

Commit 2d927cc

Browse files
Explain the "no-error" io::Error case
Fundamentally, querying the OS for error codes is a process that is deeply subject to the whims of chance and fortune. We can account for OS, but not for every combination of platform APIs. A compiled binary may not recognize new errors introduced years later. We should clarify a few especially odd situations, and what they mean: We can effectively promise nothing. This allows removing mention of ErrorKind::Uncategorized. That error variant is hidden quite deliberately, so we should not explicitly mention it.
1 parent 4781233 commit 2d927cc

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

library/std/src/io/error.rs

+16-2
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,7 @@ pub enum ErrorKind {
359359

360360
// "Unusual" error kinds which do not correspond simply to (sets
361361
// of) OS error codes, should be added just above this comment.
362-
// `Other` and `Uncategorised` should remain at the end:
362+
// `Other` and `Uncategorized` should remain at the end:
363363
//
364364
/// A custom error that does not fall under any other I/O error kind.
365365
///
@@ -565,6 +565,12 @@ impl Error {
565565
/// other standard library functions may call platform functions that may
566566
/// (or may not) reset the error value even if they succeed.
567567
///
568+
/// If this is used in a case where no error has yet occurred in a program,
569+
/// e.g. right after the beginning of `fn main`,
570+
/// then in principle any possible Error may be returned.
571+
/// The error code may have been set by a previous program (e.g. `execve`)
572+
/// or the OS may have initialized it to an arbitrary, even random, value.
573+
///
568574
/// # Examples
569575
///
570576
/// ```
@@ -871,6 +877,13 @@ impl Error {
871877

872878
/// Returns the corresponding [`ErrorKind`] for this error.
873879
///
880+
/// In some cases, the ErrorKind variant may not make much sense,
881+
/// either because the situation does not actually involve an error, or
882+
/// because of a new error code the standard library has not been taught.
883+
/// See [`last_os_error`] for more details.
884+
///
885+
/// [`last_os_error`]: Error::last_os_error
886+
///
874887
/// # Examples
875888
///
876889
/// ```
@@ -881,7 +894,8 @@ impl Error {
881894
/// }
882895
///
883896
/// fn main() {
884-
/// // Will print "Uncategorized".
897+
/// // As no error has occurred, this may print anything!
898+
/// // It likely prints a placeholder for unidentified (non-)errors.
885899
/// print_error(Error::last_os_error());
886900
/// // Will print "AddrInUse".
887901
/// print_error(Error::new(ErrorKind::AddrInUse, "oh no!"));

0 commit comments

Comments
 (0)