|
|
|
|
|
by airstrike
684 days ago
|
|
How so? Can't you just put your Tree in a Box? enum Tree {
Leaf,
Node(Box<Tree>, Box<Tree>),
}
fn number_of_leaves(tree: &Tree) -> u32 {
match tree {
Tree::Leaf => 1,
Tree::Node(left, right) => number_of_leaves(left) + number_of_leaves(right),
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn counts_the_number_of_leaves_in_a_tree() {
let tree_with_3_leaves = Tree::Node(
Box::new(Tree::Leaf),
Box::new(Tree::Node(
Box::new(Tree::Leaf),
Box::new(Tree::Leaf),
)),
);
let leaves = number_of_leaves(&tree_with_3_leaves);
assert_eq!(leaves, 3);
}
}
|
|
There's a sibling comment about porting it to C#. The Rust version will be less frustrating than the C#, but it is the same kind of bad.
On Rust specifically, this happens because Rust is a low level language.