Rust

Tokio & Async I/O

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

22 câu hỏi phỏng vấn·
Senior
1

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

Câu trả lời

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?

Câu trả lời

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")]?

Câu trả lời

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 câu hỏi phỏng vấn

Nắm vững Rust cho lần phỏng vấn tiếp theo

Truy cập tất cả câu hỏi, flashcards, bài kiểm tra kỹ thuật, bài tập code review và mô phỏng phỏng vấn.

Bắt đầu miễn phí