Write a 1-to-5 loop in Go and Rust
idiomatic loop syntax in Go versus Rust.
write Go's three-clause for, write Rust's 1..=5 range iterator, and contrast statement iteration with iterator consumption.
WHAT THIS TESTS: This question tests whether you can write the most basic control-flow structure idiomatically in two modern systems languages. At the senior level, it is not enough to produce working code; the interviewer is checking that you know Go has only one looping keyword and that Rust prefers iterator-based loops over index manipulation. The question also surfaces whether you understand the mechanical difference between a statement-driven C-style loop and an iterator-consuming for loop, which has implications for safety, performance, and readability.
A GOOD ANSWER COVERS: A strong response writes Go's loop as for i := 1; i <= 5; i++ and notes that Go forbids parentheses around the three clauses while requiring braces. Then it writes Rust's loop as for i in 1..=5 and explicitly uses the inclusive range syntax because 1..5 would stop at four. The candidate should then state the main syntactical difference: Go's for is a control statement that manually initializes, checks, and increments an index variable, whereas Rust's for consumes an iterator produced by the range expression, hiding the index management entirely. Mentioning that Rust's pattern binds i immutably by default adds polish.
COMMON WRONG ANSWERS: A red flag is writing Rust's loop as a while loop with a manual counter, which ignores the language's idiomatic iterator style. Another red flag is using 1..5 in Rust and forgetting it is exclusive on the upper bound, causing an off-by-one error. Some candidates write Go with parentheses around the for clauses, which is a syntax error. Claiming that both loops are basically the same C-style construct misses the semantic gap between statement-based and iterator-based iteration.
LIKELY FOLLOW-UPS: The interviewer may ask how you would iterate over a slice or vector in each language, which leads to Go's range clause versus Rust's iter or into_iter methods. They might also ask about performance, opening a discussion on whether Rust's iterator loop optimizes down to the same assembly as an index loop, which it typically does due to LLVM optimizations. A third follow-up could ask about break and continue semantics, which are similar on the surface but differ when labels or loop lifetimes are involved.
ONE CONCRETE EXAMPLE: In Go, the code reads: package main; import fmt; func main() { for i := 1; i <= 5; i++ { fmt.Println(i) } }. In Rust, the code reads: fn main() { for i in 1..=5 { println!("{}", i); } }. The Go version exposes the mechanics of the loop, while the Rust version delegates boundary logic to the inclusive range iterator. This distinction reflects Go's preference for explicit, simple control flow and Rust's preference for zero-cost abstractions that reduce manual bookkeeping.
Read the original → go.dev
Get five bites like this every day.
Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.