这篇文章上次修改于 902 天前,可能其部分内容已经发生变化,如有疑问可询问作者。
1 没有值时发生 panic
1.1 expect
- 正确,返回值;否则,中断程序,打印 msg 错误信息。
源码
pub fn expect(self, msg: &str) -> T { match self { Ok(t) => t, Err(e) => unwrap_failed(msg, &e), } }
例子
let ok: Result<&'static str, anyhow::Error> = Ok("value"); assert_eq!(ok.expect("the world is ending"), "value"); let err: Result<&'static str, anyhow::Error> = Err(anyhow::anyhow!("error")); err.expect("the world is ending"); // panics with `the world is ending`
1.2 unwrap
- 正确,返回值;否则,中断程序。
源码
pub fn unwrap(self) -> T { match self { Ok(t) => t, Err(e) => unwrap_failed("called `Result::unwrap()` on an `Err` value", &e), } }
例子
let ok: Result<&'static str, anyhow::Error> = Ok("value"); assert_eq!(ok.unwrap(), "value"); let err: Result<&'static str, anyhow::Error> = Err(anyhow::anyhow!("error")); err.unwrap(); // fails
2 错误时返回一个值
2.1 unwrap_or_default
- 正确,返回值;否则,返回默认值。
源码
pub fn unwrap_or_default(self) -> T { match self { Ok(x) => x, Err(_) => Default::default(), } }
例子
let good_year_from_input = "1909"; let good_year = good_year_from_input.parse().unwrap_or_default(); assert_eq!(1909, good_year); let bad_year_from_input = "190blarg"; let bad_year = bad_year_from_input.parse().unwrap_or_default(); assert_eq!(0, bad_year);
2.2 unwrap_or
- 正确,返回值;否则返回自定义默认值。
源码
pub fn unwrap_or(self, default: T) -> T { match self { Ok(t) => t, Err(_) => default, } }
例子
let ok: Result<&'static str, anyhow::Error> = Ok("value"); assert_eq!(ok.unwrap_or("other"), "value"); let err: Result<&'static str, anyhow::Error> = Err(anyhow::anyhow!("error")); assert_eq!(err.unwrap_or("other"), "other");
2.3 unwrap_or_else
- 正确,返回值;否则,执行闭包。
源码
pub fn unwrap_or_else<F: FnOnce(E) -> T>(self, op: F) -> T { match self { Ok(t) => t, Err(e) => op(e), } }
例子
let ok: Result<&'static str, anyhow::Error> = Ok("value"); assert_eq!(ok.unwrap_or_else(|v| "other"), "value"); let err: Result<&'static str, anyhow::Error> = Err(anyhow::anyhow!("error")); assert_eq!(err.unwrap_or_else(|v| "other"), "other");
2.4 map_or
- 正确,则执行闭包返回值;否则,返回一个自定义的默认值。
源码
pub fn map_or<U, F: FnOnce(T) -> U>(self, default: U, f: F) -> U { match self { Ok(t) => f(t), Err(_) => default, } }
例子
let ok: Result<&'static str, anyhow::Error> = Ok("value"); assert_eq!(ok.map_or(42, |v| v.len()), 5); let err: Result<&'static str, anyhow::Error> = Err(anyhow::anyhow!("error")); assert_eq!(err.map_or(42, |v| v.len()), 42);
2.5 map_or_else
- 正确,则执行第 2 个闭包;否则执行第 1 个闭包。
源码
pub fn map_or_else<U, D: FnOnce(E) -> U, F: FnOnce(T) -> U>(self, default: D, f: F) -> U { match self { Ok(t) => f(t), Err(e) => default(e), } }
例子
let k = 21; let ok: Result<&'static str, anyhow::Error> = Ok("value"); assert_eq!(ok.map_or_else(|v| 2 * k, |v| v.len()), 5); let err: Result<&'static str, anyhow::Error> = Err(anyhow::anyhow!("error")); assert_eq!(err.map_or_else(|v| 2 * k, |v| v.len()), 42);
3 错误时返回 Result
3.1 map
- 正确,则改变值,并返回另一个 Result。
源码
pub fn map<U, F: FnOnce(T) -> U>(self, op: F) -> Result<U, E> { match self { Ok(t) => Ok(op(t)), Err(e) => Err(e), } }
例子
let ok: Result<&'static str, anyhow::Error> = Ok("value"); assert_eq!(ok.map(|s| s.len()).unwrap(), 5);
3.2 map_err
- 错误,则对错误执行闭包,并返回另一个 Result。
源码
pub fn map_err<F, O: FnOnce(E) -> F>(self, op: O) -> Result<T, F> { match self { Ok(t) => Ok(t), Err(e) => Err(op(e)), } }
例子
fn stringify(x: u32) -> String { format!("error code: {}", x) } let x: Result<u32, u32> = Ok(2); assert_eq!(x.map_err(stringify), Ok(2)); let x: Result<u32, u32> = Err(13); assert_eq!(x.map_err(stringify), Err("error code: 13".to_string()));
3.2 and
- 正确,则返回另一 Result。
源码
pub fn and<U>(self, res: Result<U, E>) -> Result<U, E> { match self { Ok(_) => res, Err(e) => Err(e), } }
例子
let ok: Result<&'static str, anyhow::Error> = Ok("value"); let ok1: Result<&'static str, anyhow::Error> = Ok("other"); assert_eq!(ok.and(ok1).unwrap(), "other"); let err: Result<&'static str, anyhow::Error> = Err(anyhow::anyhow!("error")); let ok2: Result<&'static str, anyhow::Error> = Ok("other"); err.and(ok2).unwrap(); // painc
3.3 and_then
- 正确,则执行闭包。
源码
pub fn and_then<U, F: FnOnce(T) -> Result<U, E>>(self, op: F) -> Result<U, E> { match self { Ok(t) => op(t), Err(e) => Err(e), } }
例子
fn sq(x: u32) -> Result<u32, anyhow::Error> { Ok(x * x) } let ok: Result<u32, anyhow::Error> = Ok(2); assert_eq!(ok.and_then(sq).unwrap(), 4); let err: Result<u32, anyhow::Error> = Err(anyhow::anyhow!("error")); err.and_then(sq).unwrap(); // painc
3.4 or
- 错误,则返回另一个 Result。
源码
pub fn or<F>(self, res: Result<T, F>) -> Result<T, F> { match self { Ok(v) => Ok(v), Err(_) => res, } }
例子
let x: Result<u32, &str> = Ok(2); let y: Result<u32, &str> = Err("late error"); assert_eq!(x.or(y), Ok(2)); let x: Result<u32, &str> = Err("not a 2"); let y: Result<u32, &str> = Err("late error"); assert_eq!(x.or(y), Err("late error"));
3.5 or_else
- 错误,则执行闭包。
源码
pub fn or_else<F, O: FnOnce(E) -> Result<T, F>>(self, op: O) -> Result<T, F> { match self { Ok(t) => Ok(t), Err(e) => op(e), } }
例子
fn sq(x: u32) -> Result<u32, u32> { Ok(x * x) } fn err(x: u32) -> Result<u32, u32> { Err(x) } assert_eq!(Ok(2).or_else(sq).or_else(sq), Ok(2)); assert_eq!(Ok(2).or_else(err).or_else(sq), Ok(2)); assert_eq!(Err(3).or_else(sq).or_else(err), Ok(9)); assert_eq!(Err(3).or_else(err).or_else(err), Err(3));
4 返回 Option
4.1 ok
- 有值,返回值;否则返回 None。
源码
pub fn ok(self) -> Option<T> { match self { Ok(x) => Some(x), Err(_) => None, } }
例子
let ok: Result<u32, anyhow::Error> = Ok(2); assert_eq!(ok.ok(), Some(2)); let err: Result<u32, anyhow::Error> = Err(anyhow::anyhow!("error")); assert_eq!(err.ok(), None);
4.1 err
- 有值,则返回 None。
源码
pub fn err(self) -> Option<E> { match self { Ok(_) => None, Err(x) => Some(x), } }
例子
let x: Result<u32, &str> = Ok(2); assert_eq!(x.err(), None); let x: Result<u32, &str> = Err("Nothing here"); assert_eq!(x.err(), Some("Nothing here"));
没有评论