Rust

Type System

Zero-cost abstractions, newtype pattern, type aliases, phantom types, const generics

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

What is a zero-cost abstraction in Rust?

Câu trả lời

A zero-cost abstraction is an abstraction that incurs no runtime cost compared to equivalent manually written code. The Rust compiler optimizes these abstractions so that the generated code is as efficient as hand-written low-level code, with no additional overhead.

2

What is the main advantage of the newtype pattern in Rust?

Câu trả lời

The newtype pattern creates a distinct new type that wraps an existing type, providing compile-time type safety. This prevents mixing up values of the same underlying type but with different meanings, like user IDs and product IDs both represented as integers.

3

How to declare a type alias in Rust?

Câu trả lời

A type alias is declared with the type keyword followed by the alias name and target type. For example, type Kilometers = i32 creates an alias for i32. Unlike the newtype pattern, a type alias is not a distinct new type but merely a synonym that can be used interchangeably with the original type.

4

What is the difference between a type alias and a newtype in terms of type safety?

5

What is a phantom type in Rust?

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