用法
pub fn map_err<F, O:FnOnce(E) -> F>(self, op:O) -> Result<T, F>
通过将函数应用于包含的 Err
值,将 Result<T, E>
映射到 Result<T, F>
,保持 Ok
值不变。
此函数可用于在处理错误时传递成功的结果。
例子
基本用法:
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()));
相关用法
- Rust core::result::Result.map_or用法及代码示例
- Rust core::result::Result.map_or_else用法及代码示例
- Rust core::result::Result.map用法及代码示例
- Rust core::result::Result.into_ok用法及代码示例
- Rust core::result::Result.expect_err用法及代码示例
- Rust core::result::Result.is_ok用法及代码示例
- Rust core::result::Result.as_ref用法及代码示例
- Rust core::result::Result.into_ok_or_err用法及代码示例
- Rust core::result::Result.or用法及代码示例
- Rust core::result::Result.iter_mut用法及代码示例
- Rust core::result::Result.expect用法及代码示例
- Rust core::result::Result.unwrap用法及代码示例
- Rust core::result::Result.cloned用法及代码示例
- Rust core::result::Result.or_else用法及代码示例
- Rust core::result::Result.unwrap_or用法及代码示例
- Rust core::result::Result.unwrap_or_default用法及代码示例
- Rust core::result::Result.transpose用法及代码示例
- Rust core::result::Result.unwrap_err用法及代码示例
- Rust core::result::Result.err用法及代码示例
- Rust core::result::Result.iter用法及代码示例
注:本文由纯净天空筛选整理自rust-lang.org大神的英文原创作品 core::result::Result.map_err。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。