Rust

Tokio & Async I/O

Tokio runtime, async tasks, select!, join!, timeout, tokio::spawn, async file/network I/O

22 preguntas de entrevista·
Senior
1

What is the main role of the Tokio runtime in an asynchronous Rust application?

Respuesta

The Tokio runtime provides an executor that polls futures until completion. It also manages a multi-threaded scheduler (by default) to distribute tasks across multiple OS threads, and a reactor for asynchronous I/O operations. Without a runtime, Rust futures do nothing as they are lazy by default.

2

How to initialize the Tokio runtime with the #[tokio::main] macro?

Respuesta

The #[tokio::main] macro transforms an async main() function into a synchronous entry point that creates a multi-threaded Tokio runtime and executes the async function. This macro automatically generates the boilerplate needed to create the runtime and block until the main future completes.

3

What is the difference between #[tokio::main] and #[tokio::main(flavor = "current_thread")]?

Respuesta

By default, #[tokio::main] creates a multi-threaded runtime that distributes tasks across a pool of worker threads. With flavor = "current_thread", the runtime executes all tasks on the main thread only, which is useful for tests, simple applications, or when sharing data between threads should be avoided.

4

What does tokio::spawn() do and what type does it return?

5

Why must futures passed to tokio::spawn() be 'static?

+19 preguntas de entrevista

Domina Rust para tu próxima entrevista

Accede a todas las preguntas, flashcards, tests técnicos, ejercicios de code review y simuladores de entrevista.

Empieza gratis