Rust

Unsafe Rust

Unsafe blocks, raw pointers (*const T, *mut T), FFI, unsafe traits, Undefined Behavior (UB), safety invariants

20 면접 질문·
Senior
1

Which keyword enables the superpowers of unsafe mode in Rust?

답변

The unsafe keyword unlocks five capabilities forbidden in safe Rust: dereferencing raw pointers, calling unsafe functions, accessing mutable static variables, implementing unsafe traits, and accessing union fields. This block signals to the compiler that the developer takes responsibility for guaranteeing memory safety.

2

How many additional capabilities does unsafe code unlock compared to safe Rust?

답변

Unsafe mode unlocks exactly five capabilities: dereferencing raw pointers, calling unsafe functions or methods, accessing or modifying mutable static variables, implementing unsafe traits, and accessing union fields. These five operations are the only differences between safe and unsafe Rust.

3

What is the correct syntax to create an immutable raw pointer to a variable in Rust?

답변

The syntax to create an immutable raw pointer is let ptr = &x as *const T. The type *const T represents an immutable raw pointer. Creating raw pointers is safe, only dereferencing requires an unsafe block. Implicit coercion can also be used with let ptr: *const T = &x.

4

What is the difference between *const T and *mut T in Rust?

5

Why is creating raw pointers allowed in safe Rust while dereferencing them is not?

+17 면접 질문

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

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

무료로 시작하기