Skip to content
tezvyn:
Menu

What does unsafe enable in Go and Rust? List two operations.

Source: doc.rust-lang.orgEasyHow cards are made

Summary

Your grasp of where each language drops memory-safety guarantees.

Key points

Go unsafe enables pointer arithmetic and type punning; Rust unsafe permits raw pointer dereferencing and FFI.

What's really being asked

This question probes whether you understand the precise line between safe code and undefined behavior in memory-safe systems languages. Interviewers want to see that you know unsafe is not a generic escape hatch but a narrowly scoped tool with specific, well-defined powers. They also want to see if you can distinguish between Go's package-level approach and Rust's keyword-based approach.

The full answer

First, define the scope in each language. In Go, the unsafe package provides unsafe.Pointer and uintptr, which allow converting between arbitrary pointer types and performing pointer arithmetic; it does not disable the garbage collector or type system globally. In Rust, the unsafe keyword unlocks five specific capabilities: dereferencing raw pointers, calling unsafe functions including extern FFI blocks, implementing unsafe traits, accessing or mutating mutable static variables, and accessing fields of a union. Second, list two distinct operations that actually require unsafe, such as dereferencing a raw pointer in Rust and performing pointer arithmetic in Go, or calling a C library function across the FFI boundary in Rust while converting an arbitrary integer to a pointer in Go. Third, emphasize that safe abstractions can wrap unsafe internals, so end users never need to write unsafe themselves.

The mistakes people make

A major red flag is claiming that unsafe turns off the borrow checker, garbage collector, or all compiler checks; in Rust, safe invariants like ownership are still enforced even inside an unsafe block, and Go's runtime protections remain active. Another mistake is saying unsafe is purely for optimization or that it is always forbidden in production; many standard library features like split_at_mut or syscall wrappers rely on unsafe internally. Confusing the two languages is also risky, such as stating Rust uses an unsafe package or Go uses an unsafe keyword.

What usually comes next

The interviewer may ask how you would audit unsafe code for soundness, or what invariants you must manually uphold when using raw pointers. They might ask for an example of an unsafe abstraction you have written or consumed, or how Miri or Go's race detector helps catch errors that unsafe can introduce. A deeper follow-up is why Rust requires unsafe for FFI but Go does not, which touches on calling convention guarantees and runtime differences.

A concrete example

Consider implementing a zero-copy conversion from a byte slice to a string in Go. Using unsafe.Pointer, you can reinterpret the slice header as a string header because their runtime layouts share a data pointer and length, avoiding an allocation. In Rust, consider implementing SplitMut on a slice: you must use unsafe to create two mutable references from one slice, but you manually prove they do not overlap, wrapping the unsafe block inside a safe function so callers cannot violate the contract.

Interview question

Which statement correctly pairs an unsafe operation with the language that requires it?

  • a.Raw pointer dereference requires unsafe in Rust; pointer arithmetic requires unsafe in GoCorrect
  • b.Calling a foreign function requires unsafe in both Rust and Go
  • c.Mutable static variables require unsafe in Go; union field access requires unsafe in Rust
  • d.Raw pointer dereference requires unsafe in Go; pointer arithmetic requires unsafe in Rust
Why?

The card states that Rust's unsafe keyword is needed to dereference raw pointers, while Go's unsafe package enables pointer arithmetic. Option D reverses these roles, and option B is wrong because Go does not require unsafe for FFI calls.

Just read this? Test yourself on what you have been reading.

Read the original → doc.rust-lang.org

Put your scrolling time to good use

Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on go — each one lists the topics its interview covers.

See open roles