Rust

Traits

Trait definition, implementation, trait bounds, associated types, derivable traits, orphan rule

22 면접 질문·
Mid-Level
1

What is a trait in Rust?

답변

A trait in Rust defines shared functionality that types can implement. It is similar to interfaces in other languages but with additional features like default methods. Traits enable polymorphism and define a contract that types must fulfill.

2

How to define a trait in Rust?

답변

A trait is defined using the trait keyword followed by the trait name and a block containing method signatures. Methods can be declared without a body (required) or with a body (default). This syntax clearly defines the contract that implementors must fulfill.

3

What syntax is used to implement a trait for a type?

답변

The syntax impl TraitName for TypeName implements a trait for a specific type. The impl block contains concrete implementations of methods defined in the trait. This separation between trait definition and implementation enables great flexibility and code reusability.

4

What is a default method in a trait?

5

Which derivable trait allows displaying a value with the {:?} formatter?

+19 면접 질문

다음 면접을 위해 Rust을 마스터하세요

모든 질문, flashcards, 기술 테스트, 코드 리뷰 연습, 면접 시뮬레이터에 접근하세요.

무료로 시작하기