this post was submitted on 22 Jan 2026
20 points (100.0% liked)
Rust
7692 readers
18 users here now
Welcome to the Rust community! This is a place to discuss about the Rust programming language.
Wormhole
Credits
- The icon is a modified version of the official rust logo (changing the colors to a gradient and black background)
founded 2 years ago
MODERATORS
you are viewing a single comment's thread
view the rest of the comments
view the rest of the comments
Breaking down what
async fnin trait does, it converts it to afn method() -> impl Future<Output=T>, which breaks further down intofn method() -> Self::__MethodRetwhere the generated associated type implementsFuture.This doesn't work for
dyn Traitbecause of the associated type (and the factmethod()needs a dyn-safeselfparameter).Instead, your methods need to return
dyn Futurein some capacity since that return type doesn't rely on associated types. That's whereasync_traitcomes in.Box<dyn Future<...>>is a dyn-safe boxed future, then it's pinned becauseasyncusually generates a self-referential type, and you need to pin it anyway to.poll()the resulting future.Edit: also, being pedantic, associated types are fine in dyn traits, but you need to specify the type for it (like
dyn Blah<Foo=i32>. Even if you could name the return type from anasync fn, it'd be different for everyimplblock, so that's not realistic here.