Distinct uses of `impl trait` result in different opaque types?

I have a similar use case like this https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=c72132a1c6fda4ce47cd2639de4c6ab4
Feel very depressed of why this can not be compiled.

The error has a hint that probably is actually what you want:

   = note: distinct uses of `impl Trait` result in different opaque types
help: consider `await`ing on both `Future`s
7  ~         foo().await
8  |     } else {
9  ~         bar().await

You wouldn't be able to return the results from either one of foo() or bar() like that without awaiting them anyway.

1 Like

The thing is that the return type of an async is not as it seems. If we define an async function's return type as T, then it desugars to impl Future<Output = T>. This is known as an opaque type, which means the actual return type is unknown and the only property of it that is guaranteed is that it implements the Future trait with the associated Output type as T.
Therefore, although foo() and bar() seem to be returning the same type, they aren't. That causes the error.


Thanks, i understand it!

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.