← Back to archive

Stdle #137 · comparisons · 2026-10-21

What does this Rust snippet output?

A past puzzle — fully playable. 4 attempts, hints on wrong guesses.

enum Tree {
Leaf(i32),
Node(Box<Tree>, Box<Tree>),
}
fn sum(t: &Tree) -> i32 {
match t {
Tree::Leaf(v) => *v,
Tree::Node(l, r) => sum(l) + sum(r),
}
}
fn main() {
let t = Tree::Node(
Box::new(Tree::Leaf(1)),
Box::new(Tree::Leaf(2)),
);
println!("{}", sum(&t));
}
Comparisons
4 attempts left

Answer & explanation