Struct Error
struct Error { ... }
The error type for I/O operations of the Read, Write, Seek, and
associated traits.
Errors mostly originate from the underlying OS, but custom instances of
Error can be created with crafted error messages and a particular value of
ErrorKind.
Implementations
impl Error
fn new<E>(kind: ErrorKind, error: E) -> Error where E: Into<Box<dyn Error + Send + Sync>>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
errorargument is an arbitrary payload which will be contained in thisError.Note that this function allocates memory on the heap. If no extra payload is required, use the
Fromconversion fromErrorKind.Examples
use ; // errors can be created from strings let custom_error = new; // errors can also be created from other errors let custom_error2 = new; // creating an error without payload (and without memory allocation) let eof_error = from;fn other<E>(error: E) -> Error where E: Into<Box<dyn Error + Send + Sync>>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::newwithErrorKind::Other.Examples
use Error; // errors can be created from strings let custom_error = other; // errors can also be created from other errors let custom_error2 = other;fn last_os_error() -> ErrorReturns an error representing the last OS error which occurred.
This function reads the value of
errnofor the target platform (e.g.GetLastErroron Windows) and will return a corresponding instance ofErrorfor 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 Error; let os_error = last_os_error; println!;fn from_raw_os_error(code: RawOsError) -> ErrorCreates a new instance of an
Errorfrom a particular OS error code.Examples
On Linux:
# if cfg!On Windows:
# if cfg!fn raw_os_error(self: &Self) -> Option<RawOsError>Returns the OS error that this error represents (if any).
If this
Errorwas constructed vialast_os_errororfrom_raw_os_error, then this function will returnSome, otherwise it will returnNone.Examples
use ;fn get_ref(self: &Self) -> Option<&dyn Error + Send + Sync + 'static>Returns a reference to the inner error wrapped by this error (if any).
If this
Errorwas constructed vianewthen this function will returnSome, otherwise it will returnNone.Examples
use ;fn get_mut(self: &mut Self) -> Option<&mut dyn Error + Send + Sync + 'static>Returns a mutable reference to the inner error wrapped by this error (if any).
If this
Errorwas constructed vianewthen this function will returnSome, otherwise it will returnNone.Examples
use ; use ; use Display;fn into_inner(self: Self) -> Option<Box<dyn Error + Send + Sync>>Consumes the
Error, returning its inner error (if any).If this
Errorwas constructed vianeworother, then this function will returnSome, otherwise it will returnNone.Examples
use ;fn downcast<E>(self: Self) -> Result<E, Self> where E: Error + Send + Sync + 'staticAttempts to downcast the custom boxed error to
E.If this
Errorcontains a custom boxed error, then it would attempt downcasting on the boxed error, otherwise it will returnErr.If the custom boxed error has the same type as
E, it will returnOk, otherwise it will also returnErr.This method is meant to be a convenience routine for calling
Box<dyn Error + Sync + Send>::downcaston the custom boxed error, returned byError::into_inner.Examples
use fmt; use io; use Error; #fn kind(self: &Self) -> ErrorKindReturns the corresponding
ErrorKindfor this error.This may be a value set by Rust code constructing custom
io::Errors, or if thisio::Errorwas sourced from the operating system, it will be a value inferred from the system's error encoding. Seelast_os_errorfor more details.Examples
use ;
impl Debug for Error
fn fmt(self: &Self, f: &mut Formatter<'_>) -> Result
impl Display for Error
fn fmt(self: &Self, fmt: &mut Formatter<'_>) -> Result
impl Error for Error
fn cause(self: &Self) -> Option<&dyn Error>fn source(self: &Self) -> Option<&dyn Error + 'static>
impl Freeze for Error
impl From for Error
fn from(_: NulError) -> ErrorConverts a
alloc::ffi::NulErrorinto aError.
impl From for Error
impl From for Error
fn from(err: TryLockError) -> Error
impl From for Error
fn from(_: TryReserveError) -> ErrorConverts
TryReserveErrorto an error withErrorKind::OutOfMemory.TryReserveErrorwon't be available as the errorsource(), but this may change in the future.
impl RefUnwindSafe for Error
impl Send for Error
impl Sync for Error
impl Unpin for Error
impl UnsafeUnpin for Error
impl UnwindSafe for Error
impl<T> Any for Error
fn type_id(self: &Self) -> TypeId
impl<T> Borrow for Error
fn borrow(self: &Self) -> &T
impl<T> BorrowMut for Error
fn borrow_mut(self: &mut Self) -> &mut T
impl<T> From for Error
fn from(t: T) -> TReturns the argument unchanged.
impl<T> ToString for Error
fn to_string(self: &Self) -> String
impl<T, U> Into for Error
fn into(self: Self) -> UCalls
U::from(self).That is, this conversion is whatever the implementation of
[From]<T> for Uchooses to do.
impl<T, U> TryFrom for Error
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
impl<T, U> TryInto for Error
fn try_into(self: Self) -> Result<U, <U as TryFrom<T>>::Error>
impl<W> From for Error
fn from(iie: IntoInnerError<W>) -> Error