Advanced
Rust Memory Safety Refactoring
Refactor a hypothetical C++ snippet into idiomatic Rust while explaining memory management trade-offs.
📝 Prompt Content
Given a hypothetical C++ code snippet involving manual memory management, raw pointers, and potential race conditions in a multi-threaded environment, refactor it into idiomatic Rust. Specifically, utilize ownership, borrowing, and lifetimes to guarantee memory safety at compile time. After providing the Rust code, write a comparative analysis explaining how the Rust compiler prevents the specific memory corruption bugs present in the original C++ code, such as use-after-free or data races. Discuss the trade-offs regarding developer ergonomics and runtime performance.