Closed jwhance closed 3 years ago
Hi @jwhance, thanks for testing out jsonwebtokens!
I think the detail missing here is that your main() function doesn't currently return a Result
. The '?' operator is really just syntax sugar for checking whether a Result (or Option) is Ok (or Some) and if not it immediately returns from the current function with the Err (or None).
If you were to change how you start your main function from
fn main() {
to
fn main() -> Result<(), jwt::error::Error>{
I think it might then work for you as you were expecting.
Here are some more details about this: https://doc.rust-lang.org/edition-guide/rust-2018/error-handling-and-panics/question-mark-in-main-and-tests.html
Since returning jwt::error::Error
would be quite specific (it would then only work for returning jsonwwebtokens errors) you might want to consider using something like: anyhow for a convenient dynamic error / result type that can be used like this:
use anyhow::Result;
fn main() -> Result<()> {
Hope that helps!
Yes, that did help thanks! Clearly a Rust noob issue but I don't think I'd have ever made the connection from that error to my main() fn.
Cool, no worries!
Relative Rust newcomer but the examples seem to be broken. You have the code:
But doing a
cargo build
on this leads to errors on both lines ending with ?; The errors are:error[E0277]: the
?operator can only be used in a function that returns
Resultor
Option(or another type that implements
Try)
I had better results doing it like this: