Understanding Memory Management, Part 5: Fighting with Rust
Published on: 2025-07-22 21:01:23
Understanding Memory Management, Part 5: Fighting with Rust
This is the fifth post in my planned multipart series on memory management. You will probably want to go back and read Part I, which covers C, parts II and III, which cover C++, and part IV, which introduces Rust memory management. In part IV, we got through the basics of Rust memory management up through smart pointers. In this post I want to look at some of the gymnastics you need to engage in to do serious work in Rust.
Unexpected Moves #
Consider the following simple Rust code:
fn main ( ) {
let x = vec! [ 1 , 2 ] ;
for y in x {
println! ( "{}" , y ) ;
}
println! ( "{}" , x . len ( ) ) ;
}
This is straightforward: we create a vector containing the values [1, 2] , then iterate over it and print each element, and then finally print out the length of the vector. This is the kind of code people write every day. Let's see what happens when we compile it.
Error[E0382]: borrow of moved value: `x`
--> iter_into.rs:7
... Read full article.