Puzzle 12: Amnesia

Let’s solve this memory safety puzzle in Rust.

We'll cover the following

Guess the output

Before moving on to the explanation, try to guess the output of the following program. Good luck!

fn main() {
loop {
let buffer = (0..1000).collect::<Vec<u32>>();
std::mem::forget(buffer);
print!(".");
}
}

Quiz

Q

What is the output of the code above?

A)

The program loop runs forever since there’s no break keyword to stop it.

B)

The program prints a thousand dots (.) and exits.

C)

The program prints dots (.) for 10 seconds and then stops executing.