Why can't I use question mark here, as it returns a Result?

use anyhow::{Context as _, Result, anyhow};

fn main() {
    let closure = | x: i32 | -> anyhow::Result<()> {
        if x > 5 {
            return Err(anyhow!("bla"))
        } else {
            return Ok(())
        } 
    };
    
    let x = closure(9)?;
}

It won't compile, the error I get is "the ? operator can only be used in a function that returns Result or Option (or another type that implements FromResidual)", even tho the closure return type is Result!

Key word is "in". main doesn't return Result (you can add a return type so it returns Result).

2 Likes

you are absolutely right. haha. thank you.

... and the return type is ???

1 Like