What this Rust unsafe-code reasoning question tests
This medium-difficulty question asks you to evaluate the correctness and safety of an unsafe block in Rust. It is typical of interview problems at firms that use Rust for systems programming, where understanding the invariants that underpin unsafe code is critical.
To answer, you must reason about whether the code upholds the preconditions that Rust's type system normally enforces but that unsafe blocks bypass. This includes pointer validity, mutability guarantees, alignment, and initialization. The question rewards candidates who can distinguish between code that compiles and code that actually respects Rust's memory-safety contract.
- Unsafe preconditions and when they are satisfied
- Pointer dereferencing rules and lifetime validity
- Mutable and immutable borrow semantics in unsafe contexts
- Undefined behaviour vs. compile-time safety