5 ms·
the simplest way to illustrate the kind of pain that can arise is, make a binary tree from scratch. this is like a 10 second job in languages like C# or F#, bu
by jackmott 10y ago
the simplest way to illustrate the kind of pain that can arise is, make a binary tree from scratch.
this is like a 10 second job in languages like C# or F#, but really weird in Rust.
And analogous things can come up with any graph that isn't directed.
- winstonewert 10y agoI've had no issues with tree structures: enum BinaryTree { Leaf(i32), Node(BinaryTree, BinaryTree) } But perhaps there are some operations you are doing on the tree that are more difficult? Also, what kind of undirected graphs do you find yourself writing? I'm curious, because I rarely if ever find myself needing a graph.
- m1el 10y agoThis struct doesn't compile: recursive type `BinaryTree` has infinite size
- steveklabnik 10y agoSimplest solution: enum BinaryTree { Leaf(i32), Node(Box<BinaryTree>, Box<BinaryTree>) }
- winstonewert 10y agoMy bad. In my actual code I was thinking of it was a Vec<>, because my nodes aren't binary.
- dvdkon 10y agoSure, but writing a graph (any kind, but even more so undirected) in a language without GC is a minefield from the standpoint of memory safery. You simply need some kind of reference counting. High-level languages like C# and F# hide this need behind using GC for everything, but Rust tries to be lower level than those and therefore exposes the extra layer.