ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

【Rust】结果枚举(三)

2022-05-28 14:00:44  阅读:34  来源: 互联网

标签:unwrap 44 结果 枚举 let result main Rust Result


环境

  • Time 2022-03-25
  • Rust 1.59.0

示例

or

fn main() {
    let result: Result<u32, u32> = Ok(44);
    println!("{:?}", result.or(Err(4)));
}

or_else

fn main() {
    let result: Result<u32, u32> = Ok(44);
    let result: Result<u32, u32> = result.or_else(|e| Ok(e * 2));
    println!("{:?}", result);
}

unwrap_or

fn main() {
    let result: Result<u32, u32> = Err(44);
    println!("{:?}", result.unwrap_or(4));
}

unwrap_or_else

fn main() {
    let result: Result<u32, u32> = Err(44);
    println!("{:?}", result.unwrap_or_else(|x| x * 2));
}

unwrap_unchecked

fn main() {
    let result: Result<u32, u32> = Ok(44);
    println!("{:?}", unsafe { result.unwrap_unchecked() });
}

unwrap_err_unchecked

fn main() {
    let result: Result<u32, u32> = Err(44);
    println!("{:?}", unsafe { result.unwrap_err_unchecked() });
}

总结

了解了结果枚举中相关的一些方法。

附录

标签:unwrap,44,结果,枚举,let,result,main,Rust,Result
来源: https://www.cnblogs.com/jiangbo4444/p/16320526.html

专注分享技术,共同学习,共同进步。侵权联系[admin#icode9.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有