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, технічних тестів, вправ code review та симуляторів співбесід.

Почни безкоштовно