Asynchronous Programming
Async Rust compiles async fn into state machines driven by a runtime the language does not ship. Interviewers test polling, Pin, Send futures, async traits and cancellation.
on this pageshowhide
explore
- Futures and Polling5 questions
- Async Functions and Blocks5 questions
- Pin and Unpin5 questions
- Send Futures and Spawning5 questions
- Awaitable Trait Methods5 questions
- Cancellation and Drop5 questions
- Runtimes and Blocking5 questions
- Streams and Concurrent Futures5 questions
questions
page 2 of 2A Rust CLI drives a Tokio-based library call with `futures::executor::block_on` and panics with "there is no reactor running"; why, and how do you fix it?
basics
~20 sTokio's I/O and timer types need a Tokio runtime's driver in the current context; futures' block_on is a bare executor that sets none up, so creating the timer panics. Drive the call with that runtime's own block_on.
In Rust, why is `match topics.lock().unwrap().get(&room).cloned() { Some(t) => notify(t).await, None => {} }` not `Send`, despite cloning the value out?
basics
~20 sThe MutexGuard returned by lock().unwrap() is a temporary in the match scrutinee, and such temporaries live until the match has finished. The guard is therefore still alive at notify(t).await, which makes the future not Send even though t is an owned clone.
A Rust loop runs `while let Some(r) = set.next().await` over a `FuturesUnordered` and awaits slow work for each result; why do the set's other futures stall?
basics
~10 sA FuturesUnordered makes progress only while someone polls it. While the loop body awaits other work, nothing calls poll_next, so the remaining futures stay suspended at their current .await even after their wakers fire.
As a Rust library lead publishing a `Storage` trait that third parties implement, used both generically and as `Box<dyn ...>` on multi-threaded executors, how would you shape its async methods?
basics
~20 sDecide Send and dyn support up front; both are SemVer commitments. A common shape: methods returning impl Future + Send, plus a boxed dyn-compatible companion trait via a blanket impl; async-trait when simplicity or an old MSRV outweighs per-call allocation.
As the lead of a Rust async service, what team rule would you set for which operations must be cancellation-safe and how cleanup is structured?
basics
~20 sRequire every shared async method to document its cancellation safety, let timeouts and races drop only safe ones, keep resumable state outside futures, and order effects so a drop means a retry. Async cleanup goes in explicit close methods.
Your team is moving a Rust CLI tool and a high-traffic HTTP API to async; how do you decide which should go async at all, and on which runtime?
basics
~20 sGo async where many I/O waits overlap, like the API; a sequential CLI is simpler blocking, with block_on only for async-only libraries. Pick the runtime your libraries need: Tokio for most servers, smol for small tools, Embassy on microcontrollers.
For a latency-sensitive Rust service, how do you choose between a work-stealing multi-threaded executor requiring `Send` futures and thread-per-core local executors accepting `!Send` ones?
basics
~20 sDefault to a work-stealing executor with Send futures: it balances uneven load and fits most libraries. Choose thread-per-core local executors only when measurements show cross-thread synchronisation dominates and the work can be sharded evenly, accepting Rc-based state and manual balancing.
In Rust, what does the IntoFuture trait change about .await, and why would a request builder implement it instead of Future?
basics
~20 sSince Rust 1.64, .await first calls IntoFuture::into_future on its operand; every Future gets that for free. A builder implements IntoFuture so callers can chain settings and write .await directly, with the request future built only at that point.
In Rust, what does the Future contract allow when poll is called again on a future that has already returned Poll::Ready?
basics
~20 sAlmost anything except undefined behaviour: the Future docs say a second poll after Ready may panic, block forever or misbehave. Compiler-generated async futures and std's Ready panic; the futures crate's fuse() makes extra polls return Pending.
In a hand-written Rust future combinator `CountPolls<F>` wrapping an inner future, why does `self.inner.poll(cx)` fail, and how does pin projection fix it?
basics
~20 spoll needs Pin<&mut F>, but Pin<&mut Self> yields neither that nor &mut self.inner when F may be !Unpin (E0599). Project the pin: pin_project! with #[pin] inner gives Pin<&mut F> for it and &mut for the rest.
showing 31–40 of 40