Why should I prefer `Option::ok_or_else` instead of `Option::ok_or`?

前端 未结 3 876
甜味超标
甜味超标 2020-12-03 11:31

I just saw the following change in a pull request:

- .ok_or(Error::new(ErrorKind::Other, \"Decode error\"));
+ .ok_o         


        
3条回答
  •  眼角桃花
    2020-12-03 11:41

    The primary reason to use ok_or_else or any of the ..._or_else methods is to avoid executing a function when it's not needed. In the case of Option::ok_or_else or Option::unwrap_or_else, there's no need to run extra code when the Option is Some. This can make code faster, depending on what happens in the error case

    In this example, Error::new likely performs allocation, but it could also write to standard out, make a network request, or anything any piece of Rust code can do; it's hard to tell from the outside. It's generally safer to place such code in a closure so you don't have to worry about extraneous side effects when the success case happens.

    Clippy lints this for you as well:

    fn main() {
        let foo = None;
        foo.unwrap_or("hello".to_string());
    }
    
    warning: use of `unwrap_or` followed by a function call
     --> src/main.rs:3:9
      |
    3 |     foo.unwrap_or("hello".to_string());
      |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `unwrap_or_else(|| "hello".to_string())`
      |
      = note: `#[warn(clippy::or_fun_call)]` on by default
      = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#or_fun_call
    

提交回复
热议问题