Rust

Type System

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

20 preguntas de entrevista·
Senior
1

What is a zero-cost abstraction in Rust?

Respuesta

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?

Respuesta

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?

Respuesta

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