Rust Book Quiz Ch 15.5, Interior Mutability

⚓ Rust    📅 2025-07-24    👤 surdeus    👁️ 2      

surdeus

Hello!

I am currently working through the Rust Book (Brown University version). The quizzes really help a lot to keep me on my toes, but now I stumbled upon a question / solution I don't fully understand.

The final quiz of Chapter 15.5 asks which of the code snippets would violate memory safety.

I get why this would be the case in the correct answer (the vector could have been relocated due to the push, so the reference to its first element might be invalid).

But I don't understand why option 4 would be fine. My reasoning: with the first push, the vector might have been relocated, so the push with the second reference might be referencing an invalid location.

image

So I am clearly missing something here! :sweat_smile:

Could anybody give me a hint? I would really appreciate it!

Best regards!

5 posts - 3 participants

Read full topic

🏷️ Rust_feed