Unwrapping option and return if the value is None in a single line

struct Foo {
    bar:i32,
}

 enum Error {
     Error,
 }
 
 fn fun(arg:&Option<Foo>)->Result<(),Error>{
    let foo = if let Some(foo) = arg.as_ref() { //line no 14
        foo
    }
    else {
        return Err(Error::Error)
    }; //line no 19

    //proceed here
    Ok(())
 } 

I have code similar to above . I want to write line no 14 to 19 in a single line instead of loop . please help

let foo = arg.as_ref().ok_or(Error::Error)?;
5 Likes