std::io::Error - Rust (original) (raw)
Struct std::io::Error1.0.0 [−] [src]
pub struct Error { /* fields omitted */ }
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 ofError
can be created with crafted error messages and a particular value ofErrorKind.
impl [Error](../../std/io/struct.Error.html "struct std::io::Error")
[src]
`pub fn new(kind: ErrorKind, error: E) -> Error where
E: Into<Box<Error + Send + Sync>>, `[src]
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
.
use std::io::{Error, ErrorKind};
let custom_error = Error::new(ErrorKind::Other, "oh no!");
let custom_error2 = Error::new(ErrorKind::Interrupted, custom_error);Run
pub fn [last_os_error](#method.last%5Fos%5Ferror)() -> [Error](../../std/io/struct.Error.html "struct std::io::Error")
[src]
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 ofError
for the error code.
use std::io::Error;
println!("last OS error: {:?}", Error::last_os_error());Run
pub fn [from_raw_os_error](#method.from%5Fraw%5Fos%5Ferror)(code: [i32](../primitive.i32.html)) -> [Error](../../std/io/struct.Error.html "struct std::io::Error")
[src]
Creates a new instance of an Error
from a particular OS error code.
On Linux:
use std::io;
let error = io::Error::from_raw_os_error(22); assert_eq!(error.kind(), io::ErrorKind::InvalidInput);Run
On Windows:
use std::io;
let error = io::Error::from_raw_os_error(10022); assert_eq!(error.kind(), io::ErrorKind::InvalidInput);Run
pub fn [raw_os_error](#method.raw%5Fos%5Ferror)(&self) -> [Option](../../std/option/enum.Option.html "enum std::option::Option")<[i32](../primitive.i32.html)>
[src]
Returns the OS error that this error represents (if any).
If this Error
was constructed via last_os_error
orfrom_raw_os_error
, then this function will return Some
, otherwise it will return None
.
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() {
print_os_error(&Error::last_os_error());
print_os_error(&Error::new(ErrorKind::Other, "oh no!"));
}Run
pub fn [get_ref](#method.get%5Fref)(&self) -> [Option](../../std/option/enum.Option.html "enum std::option::Option")<&([Error](../../std/error/trait.Error.html "trait std::error::Error") + [Send](../../std/marker/trait.Send.html "trait std:📑:Send") + [Sync](../../std/marker/trait.Sync.html "trait std:📑:Sync") + 'static)>
1.3.0
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
.
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() {
print_error(&Error::last_os_error());
print_error(&Error::new(ErrorKind::Other, "oh no!"));
}Run
pub fn [get_mut](#method.get%5Fmut)(&mut self) -> [Option](../../std/option/enum.Option.html "enum std::option::Option")<&mut ([Error](../../std/error/trait.Error.html "trait std::error::Error") + [Send](../../std/marker/trait.Send.html "trait std:📑:Send") + [Sync](../../std/marker/trait.Sync.html "trait std:📑:Sync") + 'static)>
1.3.0
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
.
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 { fn description(&self) -> &str { &self.v } }
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::().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() {
print_error(&change_error(Error::last_os_error()));
print_error(&change_error(Error::new(ErrorKind::Other, MyError::new())));
}Run
pub fn [into_inner](#method.into%5Finner)(self) -> [Option](../../std/option/enum.Option.html "enum std::option::Option")<[Box](../../std/boxed/struct.Box.html "struct std::boxed::Box")<[Error](../../std/error/trait.Error.html "trait std::error::Error") + [Send](../../std/marker/trait.Send.html "trait std:📑:Send") + [Sync](../../std/marker/trait.Sync.html "trait std:📑:Sync")>>
1.3.0
Consumes the Error
, returning its inner error (if any).
If this Error
was constructed via new
then this function will return Some
, otherwise it will return None
.
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() {
print_error(Error::last_os_error());
print_error(Error::new(ErrorKind::Other, "oh no!"));
}Run
pub fn [kind](#method.kind)(&self) -> [ErrorKind](../../std/io/enum.ErrorKind.html "enum std::io::ErrorKind")
[src]
Returns the corresponding ErrorKind
for this error.
use std::io::{Error, ErrorKind};
fn print_error(err: Error) { println!("{:?}", err.kind()); }
fn main() {
print_error(Error::last_os_error());
print_error(Error::new(ErrorKind::AddrInUse, "oh no!"));
}Run
impl [From](../../std/convert/trait.From.html "trait std::convert::From")<[NulError](../../std/ffi/struct.NulError.html "struct std::ffi::NulError")> for [Error](../../std/io/struct.Error.html "struct std::io::Error")
[src]
impl<W> [From](../../std/convert/trait.From.html "trait std::convert::From")<[IntoInnerError](../../std/io/struct.IntoInnerError.html "struct std::io::IntoInnerError")<W>> for [Error](../../std/io/struct.Error.html "struct std::io::Error")
[src]
impl [Debug](../../std/fmt/trait.Debug.html "trait std::fmt::Debug") for [Error](../../std/io/struct.Error.html "struct std::io::Error")
[src]
impl [From](../../std/convert/trait.From.html "trait std::convert::From")<[ErrorKind](../../std/io/enum.ErrorKind.html "enum std::io::ErrorKind")> for [Error](../../std/io/struct.Error.html "struct std::io::Error")
1.14.0
Intended for use for errors not exposed to the user, where allocating onto the heap (for normal construction via Error::new) is too costly.