Description
openedon Jul 28, 2019
I got the following error today:
error[E0277]: the `?` operator can only be used in a function that returns `Result` or `Option` (or another type that implements `std::ops::Try`)
--> src/main.rs:29:20
|
29 | let mut resp = reqwest::get("...")?;
| ^^^^^^^^^^^^^^^^^^^^ cannot use the `?` operator in a function that returns `()`
|
= help: the trait `std::ops::Try` is not implemented for `()`
= note: required by `std::ops::Try::from_error`
I was confused for a minute as I misinterpreted what the error was saying. I double-checked that reqwest::get
does indeed return a Result
but a moment later I realized that it's the function that's containing reqwest::get
I should be looking at which indeed did return ()
after which I fixed the issue.
I believe this error message could be somehow improved and/or worded differently to put more stress on the containing function - after all, that's what I should've looked at (maybe the containing function should be even shown in the error message like reqwest::get
is?) though I can see how it can not always be the case. However, I do still think both the containing method's return type and the reqwest::get
call should be highlighted for the best experience and to easily allow a full understanding of what's happening.