Rust

Iterators & Closures

Iterator trait, iterator adapters (map, filter, fold), closure syntax, move closures, Fn traits

22 câu hỏi phỏng vấn·
Mid-Level
1

Which trait must a type implement to be usable in a for loop in Rust?

Câu trả lời

The IntoIterator trait allows a type to be converted into an iterator. The for loop automatically calls into_iter() on the expression. Types like Vec, arrays and ranges implement this trait by default, which allows iterating over them directly with for.

2

Which method of the Iterator trait must be implemented?

Câu trả lời

The next() method is the only required method of the Iterator trait. It returns Option<Self::Item>, either Some(item) if there are remaining elements, or None when iteration is complete. All other methods like map, filter or collect have default implementations based on next().

3

What is the difference between iter(), iter_mut() and into_iter() on a Vec<T>?

Câu trả lời

iter() returns an iterator over immutable references (&T), iter_mut() over mutable references (&mut T), and into_iter() consumes the Vec and returns elements by value (T). The choice depends on whether you want to read, modify or take ownership of the elements.

4

What is a lazy iterator in Rust and why is it important?

5

What is the correct syntax to define a closure that takes two i32 parameters and returns their sum?

+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í