Tokio spawn type inference

In order to learn about Rust threads I'm implementing a very basic web site scraper.
The code is at the link below. But I'm stuck on getting tokio::task::spawn to work.
I imagine I'm one syntax error away from getting this working.

This is because the ? operator not only propagates the error, but also can convert it to a different type. But here Rust doesn't know what type to convert it to, since it's never explicitly specified. You can explicitly specify the type with Ok::<_, SomeErrorType>(()) on the last line, which tells Rust that the entire expression evaluates to Result<(), SomeErrorType>.

1 Like

Thanks, that works! Is this the best way to specify the type? Why can't it just infer the type from the error type of the process_site method?

The error could be any type that an GenericError can be converted into, and this does not uniquely identify an error type.

Is this the best way to specify the type?

Personally, I prefer annotating the type of handles:

let mut handles: Vec<JoinHandle<Result<(), GenericError>>> = Vec::new();

But this is just my opinion. Practically speaking, both ways work fine.

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.