From 6d9e2284d848edd5c17c3963b5bcad1884a53941 Mon Sep 17 00:00:00 2001 From: Sylvestre Ledru Date: Sun, 2 Aug 2026 21:12:00 +0200 Subject: [PATCH] uucore: document strip_errno with an example --- src/uucore/src/lib/mods/error.rs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/uucore/src/lib/mods/error.rs b/src/uucore/src/lib/mods/error.rs index b4e35928c68..7786039875b 100644 --- a/src/uucore/src/lib/mods/error.rs +++ b/src/uucore/src/lib/mods/error.rs @@ -459,6 +459,24 @@ impl Display for UIoError { } /// Strip the trailing " (os error XX)" from io error strings. +/// +/// Rust renders OS errors as `"No such file or directory (os error 2)"`, while GNU +/// coreutils only prints the `strerror` part. Use this when formatting a message +/// that has to match GNU's output. +/// +/// # Examples +/// +/// ``` +/// use std::io::{Error, ErrorKind}; +/// use uucore::error::strip_errno; +/// +/// let err = Error::from_raw_os_error(2); +/// assert_eq!(strip_errno(&err), "No such file or directory"); +/// +/// // Errors without an errno are returned unchanged. +/// let err = Error::new(ErrorKind::Other, "custom failure"); +/// assert_eq!(strip_errno(&err), "custom failure"); +/// ``` pub fn strip_errno(err: &std::io::Error) -> String { let mut msg = err.to_string(); if let Some(pos) = msg.find(" (os error ") {