Skip to content
tezvyn:
Menu

How does Go's variable declaration and mutability differ from Rust?

Source: go.devEasyHow cards are made

Summary

Mutability defaults and syntax.

Key points

Contrast Rust let (immutable) and let mut (mutable) with Go var and := (mutable), noting Go uses const for immutability.

Watch out for

Claiming Go variables are immutable or that := behaves like const.

What's really being asked

This question checks whether you understand how two modern systems languages handle mutability defaults and what syntax they expose. Interviewers want to see that you know Rust enforces immutability by default as a memory-safety mechanism, while Go prioritizes brevity and programmer convenience by making variables mutable unless explicitly declared as constants. They also want to hear the exact keywords and operators: var, :=, and const in Go versus let and let mut in Rust.

The full answer

Four things in order. First, state that Rust binds variables with let and makes them immutable by default; you must write let mut to allow reassignment. Second, explain that Go takes the opposite approach: variables declared with var or the short declaration operator := are mutable by default and can be reassigned without extra syntax. Third, note that Go uses const for values that cannot change, and const declarations require compile-time determinable values and cannot use the := operator. Fourth, briefly mention the philosophical difference: Rust forces you to opt into mutability to prevent accidental side effects and data races, while Go treats mutability as the common case and keeps syntax minimal.

The mistakes people make

Claiming that Go variables are immutable by default. Confusing the short declaration operator := with a constant declaration. Asserting that Go has no immutability mechanism at all. Stating that var in Go makes variables immutable while := makes them mutable. Mixing up the syntax by saying Go uses let or that Rust uses var.

What usually comes next

How does Go's const interact with composite types like slices or maps? Why does Rust require mut to be explicit at the binding site rather than the usage site? When would you prefer var over := inside a function in Go? How does Rust's ownership model interact with mutability in a way that Go's garbage collector does not? Can you mutate a slice or map in Go even if the variable holding it is not reassigned, and how does that compare to Rust's borrow checker?

A concrete example

In Rust, let x = 5 creates an immutable binding; attempting x = 10 causes a compile error unless you originally wrote let mut x = 5. In Go, x := 5 or var x = 5 creates a variable that you can freely reassign with x = 10 later. If you want true immutability in Go, you must write const x = 5, which prevents reassignment entirely and requires the value to be known at compile time.

Interview question

If you write let x = 5 in Rust and x := 5 in Go, which statement about mutability is correct?

  • a.Both allow reassignment, but Rust requires the mut keyword only when reassigning the value
  • b.Both declarations create immutable bindings by default
  • c.The Rust declaration is immutable, while the Go declaration allows reassignment without extra keywordsCorrect
  • d.In Go, var creates an immutable binding, while the short declaration operator creates a mutable one
Why?

Rust makes bindings immutable by default, so let x = 5 cannot be reassigned without let mut, while Go variables declared with := or var are mutable by default. Option D is a common misconception because both var and := create mutable bindings in Go, and true immutability requires const.

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

Read the original → go.dev

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