Struct std::io::Error 1.0.0[−][src]
pub struct Error { /* fields omitted */ }
Expand description
Implementations
根据已知的错误以及任意的错误有效载荷创建新的 I/O 错误。
此函数通常用于创建 I/O 错误,这些错误并非源于操作系统本身。
error
参数是将包含在此 Error
中的任意有效载荷。
如果不需要额外的有效载荷,请使用来自 ErrorKind
的 From
转换。
Examples
use std::io::{Error, ErrorKind};
// 可以从字符串创建错误
let custom_error = Error::new(ErrorKind::Other, "oh no!");
// 错误也可以从其他错误中创建
let custom_error2 = Error::new(ErrorKind::Interrupted, custom_error);
// 在没有有效载荷的情况下创建错误
let eof_error = Error::from(ErrorKind::UnexpectedEof);
Run从任意错误有效载荷创建新的 I/O 错误。
此函数通常用于创建 I/O 错误,这些错误并非源于操作系统本身。
它是 Error::new
与 ErrorKind::Other
的快捷方式。
Examples
#![feature(io_error_other)]
use std::io::Error;
// 可以从字符串创建错误
let custom_error = Error::other("oh no!");
// 错误也可以从其他错误中创建
let custom_error2 = Error::other(custom_error);
Run返回此错误表示的操作系统错误 (如果有)。
如果此 Error
是通过 last_os_error
或 from_raw_os_error
构造的,则此函数将返回 Some
,否则它将返回 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() {
// 将打印 "raw OS error: ..."。
print_os_error(&Error::last_os_error());
// 将打印 "Not an OS error"。
print_os_error(&Error::new(ErrorKind::Other, "oh no!"));
}
Run返回对此错误包装的内部错误 (如果有) 的引用。
如果此 Error
是通过 new
构造的,则此函数将返回 Some
,否则它将返回 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() {
// 将打印 "No inner error"。
print_error(&Error::last_os_error());
// 将打印 "Inner error: ..."。
print_error(&Error::new(ErrorKind::Other, "oh no!"));
}
Run返回对此错误包装的内部错误的可变引用 (如果有)。
如果此 Error
是通过 new
构造的,则此函数将返回 Some
,否则它将返回 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() {
// 将打印 "No inner error"。
print_error(&change_error(Error::last_os_error()));
// 将打印 "Inner error: ..."。
print_error(&change_error(Error::new(ErrorKind::Other, MyError::new())));
}
Run消耗 Error
,并返回其内部错误 (如果有)。
如果此 Error
是通过 new
构造的,则此函数将返回 Some
,否则它将返回 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() {
// 将打印 "No inner error"。
print_error(Error::last_os_error());
// 将打印 "Inner error: ..."。
print_error(Error::new(ErrorKind::Other, "oh no!"));
}
RunTrait Implementations
👎 Deprecated since 1.42.0:
use the Display impl or to_string()
👎 Deprecated since 1.33.0:
replaced by Error::source, which can support downcasting
旨在用于未暴露给用户的错误,因为分配到堆上 (通过 Error::new 进行常规构建) 的代价太高了。
执行转换。