
async/await
async functions, Future trait, tokio runtime, async blocks, .await syntax, Streams, Pin<T>, cancellation, executor patterns
1What does a function declared with the async keyword return in Rust?
What does a function declared with the async keyword return in Rust?
답변
An async function automatically returns a type that implements the Future trait. The compiler transforms the function body into a state machine that implements Future. The return value is not computed immediately: it will only be calculated when the Future is awaited with .await or executed by a runtime.
2What is the main role of the Future trait in Rust?
What is the main role of the Future trait in Rust?
답변
The Future trait represents an asynchronous computation that may not be finished yet. Its poll method allows the executor to check if the Future is ready (Poll::Ready) or needs to wait (Poll::Pending). This is the fundamental mechanism of asynchrony in Rust, enabling non-blocking and efficient execution.
3Why is a runtime like tokio necessary to execute async code in Rust?
Why is a runtime like tokio necessary to execute async code in Rust?
답변
Rust does not include an async runtime in its standard library to keep the language minimal and allow choosing the runtime based on needs. A runtime like tokio provides the executor that polls Futures, the scheduler to manage tasks, and async I/O. Without a runtime, Futures would never be executed.
What is the difference between an async block and an async function in Rust?
What happens when using .await on a Future in Rust?
+19 면접 질문