Rust's Type System Is All You Need
This Medium article argues that much Rust code is written poorly. It compares a stack-based solution and a recursive solution for finding the maximum depth of a binary tree, and says the recursive version still repeats code and calls Rc.clone() in a way that has side effects.
The author then computes the same depth using only types. Numbers are defined as unit structs[2], where Zero stands for 0 and Succ<N> stands for N + 1. A Max trait[3] returns the larger of two numbers, and a MaxDepth trait computes the depth of a tree built from Node and Nil.
A sample tree of depth 3 compiles to Succ<Succ<Succ<Zero>>>, which the author reads as 3. Because this happens at compile time[1], the result is fixed before the program runs, so the author notes the approach works only when the tree is known at compile time. For trees known only at runtime, he says there is no clean solution yet and shows a helper file that begins with code using std::fs and std::process::Command.
Read the original medium.comDiscussion
Sources
-
Medium
Rust's Type System is All You Need


