当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Rust core::result::Result.unwrap_or_else用法及代码示例


用法

pub fn unwrap_or_else<F:FnOnce(E) -> T>(self, op:F) -> T

返回包含的 Ok 值或从闭包计算它。

例子

基本用法:

fn count(x:&str) -> usize { x.len() }

assert_eq!(Ok(2).unwrap_or_else(count), 2);
assert_eq!(Err("foo").unwrap_or_else(count), 3);

相关用法


注:本文由纯净天空筛选整理自rust-lang.org大神的英文原创作品 core::result::Result.unwrap_or_else。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。