Rust

async/await

async functions, Future trait, tokio runtime, async blocks, .await syntax, Streams, Pin<T>, cancellation, executor patterns

22 面接問題·
Mid-Level
1

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.

2

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.

3

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.

4

What is the difference between an async block and an async function in Rust?

5

What happens when using .await on a Future in Rust?

+19 面接問題

次の面接に向けてRustをマスター

すべての問題、flashcards、技術テスト、コードレビュー演習、面接シミュレーターにアクセス。

無料で始める