Option
enum Option<T> {
None,
Some(T),
}
boolに変換
is_some(&self)
is_none(&self)
値の取り出し
T
expect(self), msg: &str) -> T
unwrap(self) -> T
unwrap_or(self, default: T) -> T
unwrap_or_else<F>(self, f: F) -> T where F: FnOnce() -> T
Result<T, E>
ok_or<E>(self, err: E) -> Result<T, E>
ok_or_else<E, F>(self, err: F) -> Result<T, E> where F: FnOnce() -> E
let num = x.ok_or_else(|| 0);
Option<&T>
as_ref(&self) -> Option<&T>
as_mut(&mut self) -> Option<&mut T>
let text_length: Option<usize> = text.as_ref().map(|s| s.len());
// 中身の書き換え
match x.as_mut() {
Some(v) => *v = 42,
None => {}
}
or(self, optb: Option<T>) -> Option<T>
or_else<F>(self, f: F) -> Option<T> where F: FnOnce() -> Option<T>
xor(self, optb: Option<T>) -> Option<T>
let v = x.or(Some(0));
fn nobody() -> Option<&'static str> { None }
fn vikings() -> Option<'static str> { Some("vikings") }
let l = Some("barbarians").or_else(vikings)
let m = None.or_else(vikings) // Some("vikings")
let n = None.or_else(noobody) // None
ユーティリティ
値型の変更
map(self, f: F) -> Option<U>
map_or(self, default: U, f: F) -> U where F: FnOnce(T) -> U
map_or_else(self, default: D, f: F) -> U where D: FnOnce() -> U, F: FnOnce(T) -> U