这篇文章上次修改于 902 天前,可能其部分内容已经发生变化,如有疑问可询问作者。
1 没有值时发生 panic
1.1 expect
- 有值,返回值;否则,中断程序,打印 msg 错误信息。
源码
pub fn expect(self, msg: &str) -> T { match self { Some(val) => val, None => expect_failed(msg), }
例子
let x = Some("value"); assert_eq!(x.expect("the world is ending"), "value"); let x: Option<&str> = None; x.expect("the world is ending"); // panics with `the world is ending`
1.2 unwrap
- 有值,返回值;否则,中断程序。
源码
pub fn unwrap(self) -> T { match self { Some(val) => val, None => panic!("called `Option::unwrap()` on a `None` value"), } }
例子
let x = Some("air"); assert_eq!(x.unwrap(), "air"); let x: Option<&str> = None; assert_eq!(x.unwrap(), "air"); // fails
2 没有值时返回一个值
2.1 unwrap_or
- 有值,返回值;否则返回一个默认值。
源码
pub fn unwrap_or(self, def: T) -> T { match self { Some(x) => x, None => def, } }
例子
assert_eq!(Some("car").unwrap_or("bike"), "car"); assert_eq!(None.unwrap_or("bike"), "bike"); assert_eq!(None.unwrap_or(2), 2);
2.2 unwrap_or_else
- 有值,返回值;否则,执行闭包。
源码
pub fn unwrap_or_else<F: FnOnce() -> T>(self, f: F) -> T { match self { Some(x) => x, None => f(), } }
例子
let k = 10; assert_eq!(Some(4).unwrap_or_else(|| 2 * k), 4); assert_eq!(None.unwrap_or_else(|| 2 * k), 20); assert_eq!(None.unwrap_or_else(|| "hello"), "hello");
2.3 map_or
- 有值,则执行闭包返回值;否则,返回一个自定义的默认值。
源码
pub fn map_or<U, F: FnOnce(T) -> U>(self, default: U, f: F) -> U { match self { Some(t) => f(t), None => default, } }
例子
let x = Some("foo"); assert_eq!(x.map_or(42, |v| v.len()), 3); let x: Option<&str> = None; assert_eq!(x.map_or(42, |v| v.len()), 42);
2.4 map_or_else
- 有值,执行闭包;否则执行另一个闭包。
源码
pub fn map_or_else<U, D: FnOnce() -> U, F: FnOnce(T) -> U>(self, default: D, f: F) -> U { match self { Some(t) => f(t), None => default(), } }
例子
let k = 21; let x = Some("foo"); assert_eq!(x.map_or_else(|| 2 * k, |v| v.len()), 3); let x: Option<&str> = None; assert_eq!(x.map_or_else(|| 2 * k, |v| v.len()), 42);
3 没有值时返回 Option
3.1 map
- 改变值,并返回另一个 Option。
源码
pub fn map<U, F: FnOnce(T) -> U>(self, f: F) -> Option<U> { match self { Some(x) => Some(f(x)), None => None, } }
例子
let maybe_some_string = Some(String::from("Hello, World!")); // `Option::map` takes self *by value*, consuming `maybe_some_string` let maybe_some_len = maybe_some_string.map(|s| s.len()); assert_eq!(maybe_some_len, Some(13));
3.2 and
- 有值,返回另一 Option;否则返回 None。
源码
pub fn and<U>(self, optb: Option<U>) -> Option<U> { match self { Some(_) => optb, None => None, } }
例子
let x = Some(2); let y: Option<&str> = None; assert_eq!(x.and(y), None); let x: Option<u32> = None; let y = Some("foo"); assert_eq!(x.and(y), None); let x = Some(2); let y = Some("foo"); assert_eq!(x.and(y), Some("foo")); let x: Option<u32> = None; let y: Option<&str> = None; assert_eq!(x.and(y), None);
3.3 and_then
- 有值,执行闭包;否则返回 None。
源码
pub fn and_then<U, F: FnOnce(T) -> Option<U>>(self, f: F) -> Option<U> { match self { Some(x) => f(x), None => None, } }
例子
fn sq(x: u32) -> Option<u32> { Some(x * x) } fn nope(_: u32) -> Option<u32> { None } assert_eq!(Some(2).and_then(sq).and_then(sq), Some(16)); assert_eq!(Some(2).and_then(sq).and_then(nope), None); assert_eq!(Some(2).and_then(nope).and_then(sq), None); assert_eq!(None.and_then(sq).and_then(sq), None);
3.4 or
- 有值,返回自身;否则返回自定义的 Option。
源码
pub fn or(self, optb: Option<T>) -> Option<T> { match self { Some(_) => self, None => optb, } }
例子
let x = Some(2); let y = None; assert_eq!(x.or(y), Some(2)); let x = None; let y = Some(100); assert_eq!(x.or(y), Some(100)); let x = Some(2); let y = Some(100); assert_eq!(x.or(y), Some(2)); let x: Option<u32> = None; let y = None; assert_eq!(x.or(y), None);
3.5 or_else
- 有值,返回自身;否则执行闭包。
源码
pub fn or_else<F: FnOnce() -> Option<T>>(self, f: F) -> Option<T> { match self { Some(_) => self, None => f(), } }
例子
fn nobody() -> Option<&'static str> { None } fn vikings() -> Option<&'static str> { Some("vikings") } assert_eq!(Some("barbarians").or_else(vikings), Some("barbarians")); assert_eq!(None.or_else(vikings), Some("vikings")); assert_eq!(None.or_else(nobody), None);
3.6 take
- 取出一个值。
源码
pub fn take(&mut self) -> Option<T> { mem::replace(self, None) }
例子
let mut x = Some(2); let y = x.take(); assert_eq!(x, None); assert_eq!(y, Some(2)); let mut x: Option<u32> = None; let y = x.take(); assert_eq!(x, None); assert_eq!(y, None);
4 没有值时返回 Result
4.1 ok_or
- 有值,返回 Result;否则返回自定义的错误。
源码
pub fn ok_or<E>(self, err: E) -> Result<T, E> { match self { Some(v) => Ok(v), None => Err(err), } }
例子
let x = Some("foo"); assert_eq!(x.ok_or(0), Ok("foo")); let x: Option<&str> = None; assert_eq!(x.ok_or(0), Err(0));
4.2 ok_or_else
- 有值,返回 Result;否则执行代表错误的闭包。
源码
pub fn ok_or_else<E, F: FnOnce() -> E>(self, err: F) -> Result<T, E> { match self { Some(v) => Ok(v), None => Err(err()), } }
例子
let x = Some("foo"); assert_eq!(x.ok_or_else(|| 0), Ok("foo")); let x: Option<&str> = None; assert_eq!(x.ok_or_else(|| 0), Err(0));
没有评论