Rust Systems & Tokio (50 Qs)•Part 3: Advanced Internals, Scale & Production Patterns (Senior / Lead)
How does Async Rust work under the hood (The `Future` Trait & Poll Model)?
senior LevelCode ExampleAsync & Promises
In Simple Words: Unlike Node.js or C# which use push-based event callbacks:
- Rust uses a Pull-Based (Poll) Model:
- An async function does zero work until its
Futureis actively polled:
rustpub trait Future { type Output; fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output>; }- Returns
Poll::Ready(result)when done, orPoll::Pendingif waiting on I/O.
- An async function does zero work until its
- Waker: When an I/O event completes, the OS driver triggers the
Waker, signaling the runtime executor (Tokio) to poll the future again!
text
Rust Async Poll Pipeline:
[Tokio Executor] ──► Calls future.poll(cx) ──► Returns Poll::Pending
│ (Waiting on I/O)
▼
[OS Socket Event] ──► Waker.wake() signals runtime ──► Re-poll future! ──► Returns Poll::Ready(data)