Rust for Pythonistas

Lesson 20 of 26

Memory without a garbage collector

Every program has to give memory back when it no longer needs it. Python and Rust both do this for you, in very different ways.

  • CPython keeps a reference count on every object. When the count drops to 0, the object is freed right away. Objects that point at each other in a circle never reach 0, so a separate cycle collector (the gc module) looks for them from time to time.
  • Rust has neither. Every value has one owner (lesson 13), and the compiler puts the "free it" step where the owner ends: at its closing }. Nothing runs in the background, and there is nothing to tune.

You can watch it happen. Python calls __del__ when it frees an object; Rust calls drop from the Drop trait. The examples use both to print a line when something is freed.

Watching memory being freed

Both print the same thing, for different reasons. CPython frees an object the moment its reference count reaches 0. Rust frees a value at the } where its owner ends, decided when the program is compiled. The timing of __del__ is a CPython detail (PyPy, for example, runs it later); the timing of drop is a Rust guarantee.

Python
class Buffer:
    def __init__(self, name):
        self.name = name
        print(f"open {name}")

    def __del__(self):
        print(f"free {self.name}")


def work():
    temp = Buffer("temp")
    print("working")


big = Buffer("big")
work()
print("back from work")
del big
print("end")
Rust
struct Buffer {
    name: String,
}

impl Buffer {
    fn new(name: &str) -> Buffer {
        println!("open {name}");
        Buffer { name: String::from(name) }
    }
}

impl Drop for Buffer {
    fn drop(&mut self) {
        println!("free {}", self.name);
    }
}

fn work() {
    let _temp = Buffer::new("temp");
    println!("working");
}

fn main() {
    let big = Buffer::new("big");
    work();
    println!("back from work");
    drop(big);
    println!("end");
}
Output
open big
open temp
working
free temp
back from work
free big
end

Moving a Vec doesn't copy its items

A Vec has two parts: a small header on the stack (a pointer, the length and the capacity: 24 bytes on a 64-bit machine) and the items on the heap. let b = a; copies only the small header and makes b the owner. The items stay where they are. .clone() asks for new heap memory and copies every item. as_ptr() gives the address of the items, so we can compare.

Python
a = [1, 2, 3]
b = a
print("assign:", "same data" if b is a else "new data")
c = b.copy()
print("copy:", "same data" if c is b else "new data")
Rust
fn main() {
    let a = vec![1, 2, 3];
    let items = a.as_ptr();
    let b = a;
    println!("assign: {}", if b.as_ptr() == items { "same data" } else { "new data" });
    let c = b.clone();
    println!("copy: {}", if c.as_ptr() == b.as_ptr() { "same data" } else { "new data" });
}
Output
assign: same data
copy: new data

Box: a value on the heap, with one owner

Rust must know the size of every value when it compiles. A node that contains the next node would contain itself forever, so it has no size. Python never has this problem, because every attribute is just a reference to an object somewhere on the heap. In Rust you ask for that with Box: a Box<Node> is a pointer (8 bytes) to a Node on the heap, and the box owns it. When the box is dropped, the node is freed too.

Python
from dataclasses import dataclass


@dataclass
class Node:
    value: int
    next: "Node | None"


items = Node(1, Node(2, Node(3, None)))
total = 0
node = items
while node is not None:
    total += node.value
    node = node.next
print(total)
Rust
struct Node {
    value: i32,
    next: Option<Node>,
}

fn main() {
    let items = Node {
        value: 1,
        next: Some(Node { value: 2, next: Some(Node { value: 3, next: None }) }),
    };
    let mut total = 0;
    let mut node = Some(&items);
    while let Some(n) = node {
        total += n.value;
        node = n.next.as_ref();
    }
    println!("{total}");
}
Output
6

Where the Python analogy breaks

Cheat card

PythonRust
del xdrop(x);
def __del__(self): ...impl Drop for X { fn drop(&mut self) { ... } }
# freed when the refcount reaches 0// freed at the owner's closing }
gc.collect()// no garbage collector
b = a # same list, two nameslet b = a; // moves the header, the items stay
b = a.copy()let b = a.clone(); // new heap memory
class Node: next: "Node | None"struct Node { next: Option<Box<Node>> }
sys.getsizeof(x)std::mem::size_of::<T>()
_ = make() # a normal namelet _ = make(); // dropped at once

← Reading real code Shared ownership: Rc, RefCell and Weak →