error[E0277]: the `?` operator can only be used in a closure that returns `Result` or `Option` (or another type that implements `FromResidual`)
--> src/main.rs:8:28
|
8 | thread::spawn(|| {bar()?});
| ---------^-
| | |
| | cannot use the `?` operator in a closure that returns `()`
| this function should return `Result` or `Option` to accept `?`
You can fix your thread::spawn example by replacing || { bar()? } with
|| {
bar()?;
Ok(())
}
so that the return type of the closure is Result<(), Error>. The reason your original code doesn't work is that the type of the expression bar()? is/should be (), so the return type of the closure is inferred to be (), which is incompatible with using ?.
error[E0282]: type annotations needed
--> src/main.rs:8:31
|
8 | thread::spawn(|| {bar()?; Ok(())});
| ^^ cannot infer type for type parameter `E` declared on the enum `Result`
Ah, yeah, that's annoying. The quickest fix is to add a turbofish somewhere, either Ok::<_, Error>(()) or thread::spawn::<_, Result<_, Error>>. I think type inference falls over in this case because ? has an (unfortunate imo) implicit conversion with From::from built in, it's the same issue that causes inference problems for try blocks.