pub struct Error { /* private fields */ }alloc_io)Expand description
Implementationsยง
Sourceยงimpl Error
impl Error
1.0.0 ยท Sourcepub fn raw_os_error(&self) -> Option<i32>
pub fn raw_os_error(&self) -> Option<i32>
Returns the OS error that this error represents (if any).
If this Error was constructed via last_os_error or
from_raw_os_error, then this function will return Some, otherwise
it will return None.
ยงExamples
use std::io::{Error, ErrorKind};
fn print_os_error(err: &Error) {
if let Some(raw_os_err) = err.raw_os_error() {
println!("raw OS error: {raw_os_err:?}");
} else {
println!("Not an OS error");
}
}
fn main() {
// Will print "raw OS error: ...".
print_os_error(&Error::last_os_error());
// Will print "Not an OS error".
print_os_error(&Error::new(ErrorKind::Other, "oh no!"));
}1.3.0 ยท Sourcepub fn get_ref(&self) -> Option<&(dyn Error + Sync + Send + 'static)>
pub fn get_ref(&self) -> Option<&(dyn Error + Sync + Send + 'static)>
Returns a reference to the inner error wrapped by this error (if any).
If this Error was constructed via new then this function will
return Some, otherwise it will return None.
ยงExamples
use std::io::{Error, ErrorKind};
fn print_error(err: &Error) {
if let Some(inner_err) = err.get_ref() {
println!("Inner error: {inner_err:?}");
} else {
println!("No inner error");
}
}
fn main() {
// Will print "No inner error".
print_error(&Error::last_os_error());
// Will print "Inner error: ...".
print_error(&Error::new(ErrorKind::Other, "oh no!"));
}1.3.0 ยท Sourcepub fn get_mut(&mut self) -> Option<&mut (dyn Error + Sync + Send + 'static)>
pub fn get_mut(&mut self) -> Option<&mut (dyn Error + Sync + Send + 'static)>
Returns a mutable reference to the inner error wrapped by this error (if any).
If this Error was constructed via new then this function will
return Some, otherwise it will return None.
ยงExamples
use std::io::{Error, ErrorKind};
use std::{error, fmt};
use std::fmt::Display;
#[derive(Debug)]
struct MyError {
v: String,
}
impl MyError {
fn new() -> MyError {
MyError {
v: "oh no!".to_string()
}
}
fn change_message(&mut self, new_message: &str) {
self.v = new_message.to_string();
}
}
impl error::Error for MyError {}
impl Display for MyError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "MyError: {}", self.v)
}
}
fn change_error(mut err: Error) -> Error {
if let Some(inner_err) = err.get_mut() {
inner_err.downcast_mut::<MyError>().unwrap().change_message("I've been changed!");
}
err
}
fn print_error(err: &Error) {
if let Some(inner_err) = err.get_ref() {
println!("Inner error: {inner_err}");
} else {
println!("No inner error");
}
}
fn main() {
// Will print "No inner error".
print_error(&change_error(Error::last_os_error()));
// Will print "Inner error: ...".
print_error(&change_error(Error::new(ErrorKind::Other, MyError::new())));
}1.0.0 ยท Sourcepub fn kind(&self) -> ErrorKind
pub fn kind(&self) -> ErrorKind
Returns the corresponding ErrorKind for this error.
This may be a value set by Rust code constructing custom io::Errors,
or if this io::Error was sourced from the operating system,
it will be a value inferred from the systemโs error encoding.
See last_os_error for more details.
ยงExamples
use std::io::{Error, ErrorKind};
fn print_error(err: Error) {
println!("{:?}", err.kind());
}
fn main() {
// As no error has (visibly) occurred, this may print anything!
// It likely prints a placeholder for unidentified (non-)errors.
print_error(Error::last_os_error());
// Will print "AddrInUse".
print_error(Error::new(ErrorKind::AddrInUse, "oh no!"));
}Sourceยงimpl Error
impl Error
1.0.0 ยท Sourcepub fn last_os_error() -> Error
pub fn last_os_error() -> Error
Returns an error representing the last OS error which occurred.
This function reads the value of errno for the target platform (e.g.
GetLastError on Windows) and will return a corresponding instance of
Error for the error code.
This should be called immediately after a call to a platform function, otherwise the state of the error value is indeterminate. In particular, other standard library functions may call platform functions that may (or may not) reset the error value even if they succeed.
ยงExamples
use std::io::Error;
let os_error = Error::last_os_error();
println!("last OS error: {os_error:?}");1.0.0 ยท Sourcepub fn from_raw_os_error(code: i32) -> Error
pub fn from_raw_os_error(code: i32) -> Error
Creates a new instance of an Error from a particular OS error code.
ยงExamples
On Linux:
use std::io;
let error = io::Error::from_raw_os_error(22);
assert_eq!(error.kind(), io::ErrorKind::InvalidInput);On Windows:
use std::io;
let error = io::Error::from_raw_os_error(10022);
assert_eq!(error.kind(), io::ErrorKind::InvalidInput);Sourceยงimpl Error
impl Error
1.0.0 ยท Sourcepub fn new<E>(kind: ErrorKind, error: E) -> Error
pub fn new<E>(kind: ErrorKind, error: E) -> Error
Creates a new I/O error from a known kind of error as well as an arbitrary error payload.
This function is used to generically create I/O errors which do not
originate from the OS itself. The error argument is an arbitrary
payload which will be contained in this Error.
Note that this function allocates memory on the heap.
If no extra payload is required, use the From conversion from
ErrorKind.
ยงExamples
use std::io::{Error, ErrorKind};
// errors can be created from strings
let custom_error = Error::new(ErrorKind::Other, "oh no!");
// errors can also be created from other errors
let custom_error2 = Error::new(ErrorKind::Interrupted, custom_error);
// creating an error without payload (and without memory allocation)
let eof_error = Error::from(ErrorKind::UnexpectedEof);1.74.0 ยท Sourcepub fn other<E>(error: E) -> Error
pub fn other<E>(error: E) -> Error
Creates a new I/O error from an arbitrary error payload.
This function is used to generically create I/O errors which do not
originate from the OS itself. It is a shortcut for Error::new
with ErrorKind::Other.
ยงExamples
use std::io::Error;
// errors can be created from strings
let custom_error = Error::other("oh no!");
// errors can also be created from other errors
let custom_error2 = Error::other(custom_error);1.3.0 ยท Sourcepub fn into_inner(self) -> Option<Box<dyn Error + Sync + Send>>
pub fn into_inner(self) -> Option<Box<dyn Error + Sync + Send>>
Consumes the Error, returning its inner error (if any).
If this Error was constructed via new or other,
then this function will return Some,
otherwise it will return None.
ยงExamples
use std::io::{Error, ErrorKind};
fn print_error(err: Error) {
if let Some(inner_err) = err.into_inner() {
println!("Inner error: {inner_err}");
} else {
println!("No inner error");
}
}
fn main() {
// Will print "No inner error".
print_error(Error::last_os_error());
// Will print "Inner error: ...".
print_error(Error::new(ErrorKind::Other, "oh no!"));
}1.79.0 ยท Sourcepub fn downcast<E>(self) -> Result<E, Error>
pub fn downcast<E>(self) -> Result<E, Error>
Attempts to downcast the custom boxed error to E.
If this Error contains a custom boxed error,
then it would attempt downcasting on the boxed error,
otherwise it will return Err.
If the custom boxed error has the same type as E, it will return Ok,
otherwise it will also return Err.
This method is meant to be a convenience routine for calling
Box<dyn Error + Sync + Send>::downcast on the custom boxed error, returned by
Error::into_inner.
ยงExamples
use std::fmt;
use std::io;
use std::error::Error;
#[derive(Debug)]
enum E {
Io(io::Error),
SomeOtherVariant,
}
impl fmt::Display for E {
// ...
}
impl Error for E {}
impl From<io::Error> for E {
fn from(err: io::Error) -> E {
err.downcast::<E>()
.unwrap_or_else(E::Io)
}
}
impl From<E> for io::Error {
fn from(err: E) -> io::Error {
match err {
E::Io(io_error) => io_error,
e => io::Error::new(io::ErrorKind::Other, e),
}
}
}
let e = E::SomeOtherVariant;
// Convert it to an io::Error
let io_error = io::Error::from(e);
// Cast it back to the original variant
let e = E::from(io_error);
assert!(matches!(e, E::SomeOtherVariant));
let io_error = io::Error::from(io::ErrorKind::AlreadyExists);
// Convert it to E
let e = E::from(io_error);
// Cast it back to the original variant
let io_error = io::Error::from(e);
assert_eq!(io_error.kind(), io::ErrorKind::AlreadyExists);
assert!(io_error.get_ref().is_none());
assert!(io_error.raw_os_error().is_none());Trait Implementationsยง
1.0.0 ยท Sourceยงimpl Error for Error
impl Error for Error
Sourceยงfn cause(&self) -> Option<&dyn Error>
fn cause(&self) -> Option<&dyn Error>
replaced by Error::source, which can support downcasting
Sourceยงfn source(&self) -> Option<&(dyn Error + 'static)>
fn source(&self) -> Option<&(dyn Error + 'static)>
1.0.0 ยท Sourceยงfn description(&self) -> &str
fn description(&self) -> &str
use the Display impl or to_string()
Sourceยงimpl<T> From<AsyncFdTryNewError<T>> for Error
impl<T> From<AsyncFdTryNewError<T>> for Error
Sourceยงfn from(value: AsyncFdTryNewError<T>) -> Error
fn from(value: AsyncFdTryNewError<T>) -> Error
Sourceยงimpl<S, E> From<ConversionError<S, E>> for Errorwhere
E: Display,
Boxes the error into an io::Error, dropping the retained file descriptor in the process.
impl<S, E> From<ConversionError<S, E>> for Errorwhere
E: Display,
Boxes the error into an io::Error, dropping the retained file descriptor in the process.
Sourceยงfn from(e: ConversionError<S, E>) -> Error
fn from(e: ConversionError<S, E>) -> Error
Sourceยงimpl From<DecodeError> for Error
Available on crate feature std only.
impl From<DecodeError> for Error
std only.Sourceยงfn from(error: DecodeError) -> Error
fn from(error: DecodeError) -> Error
Sourceยงimpl From<EncodeError> for Error
Available on crate feature std only.
impl From<EncodeError> for Error
std only.Sourceยงfn from(error: EncodeError) -> Error
fn from(error: EncodeError) -> Error
Sourceยงimpl From<Error> for Error
Available on crate feature std only.
impl From<Error> for Error
std only.Sourceยงfn from(j: Error) -> Error
fn from(j: Error) -> Error
Convert a serde_json::Error into an io::Error.
JSON syntax and data errors are turned into InvalidData I/O errors.
EOF errors are turned into UnexpectedEof I/O errors.
use std::io;
enum MyError {
Io(io::Error),
Json(serde_json::Error),
}
impl From<serde_json::Error> for MyError {
fn from(err: serde_json::Error) -> MyError {
use serde_json::error::Category;
match err.classify() {
Category::Io => {
MyError::Io(err.into())
}
Category::Syntax | Category::Data | Category::Eof => {
MyError::Json(err)
}
}
}
}1.14.0 ยท Sourceยงimpl From<ErrorKind> for Error
Intended for use for errors not exposed to the user, where allocating onto
the heap (for normal construction via Error::new) is too costly.
impl From<ErrorKind> for Error
Intended for use for errors not exposed to the user, where allocating onto the heap (for normal construction via Error::new) is too costly.
1.0.0 ยท Sourceยงimpl<W> From<IntoInnerError<W>> for Error
impl<W> From<IntoInnerError<W>> for Error
Sourceยงfn from(iie: IntoInnerError<W>) -> Error
fn from(iie: IntoInnerError<W>) -> Error
Sourceยงimpl From<MultiError> for Error
impl From<MultiError> for Error
Sourceยงfn from(e: MultiError) -> Error
fn from(e: MultiError) -> Error
1.0.0 ยท Sourceยงimpl From<NulError> for Error
Available on non-no_global_oom_handling and non-no_rc and non-no_sync only.
impl From<NulError> for Error
no_global_oom_handling and non-no_rc and non-no_sync only.Sourceยงfn from(e: ShareError) -> Error
fn from(e: ShareError) -> Error
Sourceยงimpl From<TryGetError> for Error
Available on crate feature std only.
impl From<TryGetError> for Error
std only.Sourceยงfn from(error: TryGetError) -> Error
fn from(error: TryGetError) -> Error
1.89.0 ยท Sourceยงimpl From<TryLockError> for Error
impl From<TryLockError> for Error
Sourceยงfn from(err: TryLockError) -> Error
fn from(err: TryLockError) -> Error
1.78.0 ยท Sourceยงimpl From<TryReserveError> for Error
impl From<TryReserveError> for Error
Sourceยงfn from(_: TryReserveError) -> Error
fn from(_: TryReserveError) -> Error
Converts TryReserveError to an error with ErrorKind::OutOfMemory.
TryReserveError wonโt be available as the error source(),
but this may change in the future.
Auto Trait Implementationsยง
impl !RefUnwindSafe for Error
impl !UnwindSafe for Error
impl Freeze for Error
impl Send for Error
impl Sync for Error
impl Unpin for Error
impl UnsafeUnpin for Error
Blanket Implementationsยง
Sourceยงimpl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Sourceยงfn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Sourceยงimpl<T> Instrument for T
impl<T> Instrument for T
Sourceยงfn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Sourceยงfn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Sourceยงimpl<T> Instrument for T
impl<T> Instrument for T
Sourceยงfn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Sourceยงfn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Sourceยงimpl<D> OwoColorize for D
impl<D> OwoColorize for D
Sourceยงfn fg<C>(&self) -> FgColorDisplay<'_, C, Self>where
C: Color,
fn fg<C>(&self) -> FgColorDisplay<'_, C, Self>where
C: Color,
Sourceยงfn bg<C>(&self) -> BgColorDisplay<'_, C, Self>where
C: Color,
fn bg<C>(&self) -> BgColorDisplay<'_, C, Self>where
C: Color,
Sourceยงfn black<'a>(&'a self) -> FgColorDisplay<'a, Black, Self>
fn black<'a>(&'a self) -> FgColorDisplay<'a, Black, Self>
Sourceยงfn on_black<'a>(&'a self) -> BgColorDisplay<'a, Black, Self>
fn on_black<'a>(&'a self) -> BgColorDisplay<'a, Black, Self>
Sourceยงfn red<'a>(&'a self) -> FgColorDisplay<'a, Red, Self>
fn red<'a>(&'a self) -> FgColorDisplay<'a, Red, Self>
Sourceยงfn on_red<'a>(&'a self) -> BgColorDisplay<'a, Red, Self>
fn on_red<'a>(&'a self) -> BgColorDisplay<'a, Red, Self>
Sourceยงfn green<'a>(&'a self) -> FgColorDisplay<'a, Green, Self>
fn green<'a>(&'a self) -> FgColorDisplay<'a, Green, Self>
Sourceยงfn on_green<'a>(&'a self) -> BgColorDisplay<'a, Green, Self>
fn on_green<'a>(&'a self) -> BgColorDisplay<'a, Green, Self>
Sourceยงfn yellow<'a>(&'a self) -> FgColorDisplay<'a, Yellow, Self>
fn yellow<'a>(&'a self) -> FgColorDisplay<'a, Yellow, Self>
Sourceยงfn on_yellow<'a>(&'a self) -> BgColorDisplay<'a, Yellow, Self>
fn on_yellow<'a>(&'a self) -> BgColorDisplay<'a, Yellow, Self>
Sourceยงfn blue<'a>(&'a self) -> FgColorDisplay<'a, Blue, Self>
fn blue<'a>(&'a self) -> FgColorDisplay<'a, Blue, Self>
Sourceยงfn on_blue<'a>(&'a self) -> BgColorDisplay<'a, Blue, Self>
fn on_blue<'a>(&'a self) -> BgColorDisplay<'a, Blue, Self>
Sourceยงfn magenta<'a>(&'a self) -> FgColorDisplay<'a, Magenta, Self>
fn magenta<'a>(&'a self) -> FgColorDisplay<'a, Magenta, Self>
Sourceยงfn on_magenta<'a>(&'a self) -> BgColorDisplay<'a, Magenta, Self>
fn on_magenta<'a>(&'a self) -> BgColorDisplay<'a, Magenta, Self>
Sourceยงfn purple<'a>(&'a self) -> FgColorDisplay<'a, Magenta, Self>
fn purple<'a>(&'a self) -> FgColorDisplay<'a, Magenta, Self>
Sourceยงfn on_purple<'a>(&'a self) -> BgColorDisplay<'a, Magenta, Self>
fn on_purple<'a>(&'a self) -> BgColorDisplay<'a, Magenta, Self>
Sourceยงfn cyan<'a>(&'a self) -> FgColorDisplay<'a, Cyan, Self>
fn cyan<'a>(&'a self) -> FgColorDisplay<'a, Cyan, Self>
Sourceยงfn on_cyan<'a>(&'a self) -> BgColorDisplay<'a, Cyan, Self>
fn on_cyan<'a>(&'a self) -> BgColorDisplay<'a, Cyan, Self>
Sourceยงfn white<'a>(&'a self) -> FgColorDisplay<'a, White, Self>
fn white<'a>(&'a self) -> FgColorDisplay<'a, White, Self>
Sourceยงfn on_white<'a>(&'a self) -> BgColorDisplay<'a, White, Self>
fn on_white<'a>(&'a self) -> BgColorDisplay<'a, White, Self>
Sourceยงfn default_color<'a>(&'a self) -> FgColorDisplay<'a, Default, Self>
fn default_color<'a>(&'a self) -> FgColorDisplay<'a, Default, Self>
Sourceยงfn on_default_color<'a>(&'a self) -> BgColorDisplay<'a, Default, Self>
fn on_default_color<'a>(&'a self) -> BgColorDisplay<'a, Default, Self>
Sourceยงfn bright_black<'a>(&'a self) -> FgColorDisplay<'a, BrightBlack, Self>
fn bright_black<'a>(&'a self) -> FgColorDisplay<'a, BrightBlack, Self>
Sourceยงfn on_bright_black<'a>(&'a self) -> BgColorDisplay<'a, BrightBlack, Self>
fn on_bright_black<'a>(&'a self) -> BgColorDisplay<'a, BrightBlack, Self>
Sourceยงfn bright_red<'a>(&'a self) -> FgColorDisplay<'a, BrightRed, Self>
fn bright_red<'a>(&'a self) -> FgColorDisplay<'a, BrightRed, Self>
Sourceยงfn on_bright_red<'a>(&'a self) -> BgColorDisplay<'a, BrightRed, Self>
fn on_bright_red<'a>(&'a self) -> BgColorDisplay<'a, BrightRed, Self>
Sourceยงfn bright_green<'a>(&'a self) -> FgColorDisplay<'a, BrightGreen, Self>
fn bright_green<'a>(&'a self) -> FgColorDisplay<'a, BrightGreen, Self>
Sourceยงfn on_bright_green<'a>(&'a self) -> BgColorDisplay<'a, BrightGreen, Self>
fn on_bright_green<'a>(&'a self) -> BgColorDisplay<'a, BrightGreen, Self>
Sourceยงfn bright_yellow<'a>(&'a self) -> FgColorDisplay<'a, BrightYellow, Self>
fn bright_yellow<'a>(&'a self) -> FgColorDisplay<'a, BrightYellow, Self>
Sourceยงfn on_bright_yellow<'a>(&'a self) -> BgColorDisplay<'a, BrightYellow, Self>
fn on_bright_yellow<'a>(&'a self) -> BgColorDisplay<'a, BrightYellow, Self>
Sourceยงfn bright_blue<'a>(&'a self) -> FgColorDisplay<'a, BrightBlue, Self>
fn bright_blue<'a>(&'a self) -> FgColorDisplay<'a, BrightBlue, Self>
Sourceยงfn on_bright_blue<'a>(&'a self) -> BgColorDisplay<'a, BrightBlue, Self>
fn on_bright_blue<'a>(&'a self) -> BgColorDisplay<'a, BrightBlue, Self>
Sourceยงfn bright_magenta<'a>(&'a self) -> FgColorDisplay<'a, BrightMagenta, Self>
fn bright_magenta<'a>(&'a self) -> FgColorDisplay<'a, BrightMagenta, Self>
Sourceยงfn on_bright_magenta<'a>(&'a self) -> BgColorDisplay<'a, BrightMagenta, Self>
fn on_bright_magenta<'a>(&'a self) -> BgColorDisplay<'a, BrightMagenta, Self>
Sourceยงfn bright_purple<'a>(&'a self) -> FgColorDisplay<'a, BrightMagenta, Self>
fn bright_purple<'a>(&'a self) -> FgColorDisplay<'a, BrightMagenta, Self>
Sourceยงfn on_bright_purple<'a>(&'a self) -> BgColorDisplay<'a, BrightMagenta, Self>
fn on_bright_purple<'a>(&'a self) -> BgColorDisplay<'a, BrightMagenta, Self>
Sourceยงfn bright_cyan<'a>(&'a self) -> FgColorDisplay<'a, BrightCyan, Self>
fn bright_cyan<'a>(&'a self) -> FgColorDisplay<'a, BrightCyan, Self>
Sourceยงfn on_bright_cyan<'a>(&'a self) -> BgColorDisplay<'a, BrightCyan, Self>
fn on_bright_cyan<'a>(&'a self) -> BgColorDisplay<'a, BrightCyan, Self>
Sourceยงfn bright_white<'a>(&'a self) -> FgColorDisplay<'a, BrightWhite, Self>
fn bright_white<'a>(&'a self) -> FgColorDisplay<'a, BrightWhite, Self>
Sourceยงfn on_bright_white<'a>(&'a self) -> BgColorDisplay<'a, BrightWhite, Self>
fn on_bright_white<'a>(&'a self) -> BgColorDisplay<'a, BrightWhite, Self>
Sourceยงfn bold<'a>(&'a self) -> BoldDisplay<'a, Self>
fn bold<'a>(&'a self) -> BoldDisplay<'a, Self>
Sourceยงfn dimmed<'a>(&'a self) -> DimDisplay<'a, Self>
fn dimmed<'a>(&'a self) -> DimDisplay<'a, Self>
Sourceยงfn italic<'a>(&'a self) -> ItalicDisplay<'a, Self>
fn italic<'a>(&'a self) -> ItalicDisplay<'a, Self>
Sourceยงfn underline<'a>(&'a self) -> UnderlineDisplay<'a, Self>
fn underline<'a>(&'a self) -> UnderlineDisplay<'a, Self>
Sourceยงfn blink<'a>(&'a self) -> BlinkDisplay<'a, Self>
fn blink<'a>(&'a self) -> BlinkDisplay<'a, Self>
Sourceยงfn blink_fast<'a>(&'a self) -> BlinkFastDisplay<'a, Self>
fn blink_fast<'a>(&'a self) -> BlinkFastDisplay<'a, Self>
Sourceยงfn reversed<'a>(&'a self) -> ReversedDisplay<'a, Self>
fn reversed<'a>(&'a self) -> ReversedDisplay<'a, Self>
Sourceยงfn strikethrough<'a>(&'a self) -> StrikeThroughDisplay<'a, Self>
fn strikethrough<'a>(&'a self) -> StrikeThroughDisplay<'a, Self>
Sourceยงfn color<Color>(&self, color: Color) -> FgDynColorDisplay<'_, Color, Self>where
Color: DynColor,
fn color<Color>(&self, color: Color) -> FgDynColorDisplay<'_, Color, Self>where
Color: DynColor,
OwoColorize::fg or
a color-specific method, such as OwoColorize::green, Read moreSourceยงfn on_color<Color>(&self, color: Color) -> BgDynColorDisplay<'_, Color, Self>where
Color: DynColor,
fn on_color<Color>(&self, color: Color) -> BgDynColorDisplay<'_, Color, Self>where
Color: DynColor,
OwoColorize::bg or
a color-specific method, such as OwoColorize::on_yellow, Read more