API and Numeric Boundaries · 14 min · free with an account

Async Traits and dyn Compatibility

Lesson preview · about 30%

Native async trait methods and dynamic dispatch are separate interface choices.

Core idea

Check dyn compatibility, future lifetime and Send bounds independently.

Start with the idea

Rust supports async methods in traits, but a method returning a hidden future is not directly dispatchable through a trait object under the current dyn-compatibility rules. A generic caller can retain the concrete implementation and future type. A dynamic interface can instead return an explicitly erased future, commonly behind Pin<Box<dyn Future<Output = T> + Send + lifetime>>.

The example deliberately writes that erased interface rather than relying on an attribute macro. Its returned future borrows self, so the lifetime is explicit. Send is also an explicit promise in the returned future type; it is not implied for every native async trait method. Boxing adds an ownership/allocation choice whose tradeoff should match the workload.

A simple noop-waker poll demonstrates only the immediately ready example. Real pending work needs a runtime that arranges wakeups.

Keep learning, for free

The rest of this lesson is waiting for you.

Create a free account to read the complete explanation, see the infographic, explore the code, and try the exercises. All 754 lessons and algorithm steps are included.

No payment details. Your account opens the complete Academy.

← All lessons in Rust Interview Prep