Earlier quoted context omitted.
As a Rust beginner that likes to learn the hard way I think I have some insights why Rust seems cumbersome and/or hard for programmers trying it. Rust uses syntax that feels familiar but means completely different things than in pretty much any other language. For example '=' doesn't mean assign handle or copy. It by default means move. 'let' doesn't mean create a name for something. It means create physical space fo…
But Rc is only useful for creating tree-like data-structures. One non-tree cross-link or back-link and you'll have to redesign your entire code.
You can fairly easily refactor your almost-tree code to adapt it to that additional Option wrap.
Of course you might instead opt to introduce some garbage collector crate into your project. They usually provide garbage collected Rc equivalent, which makes swapping it out very easy.
Rc's are really very useful first approach to making anything complex in Rust.
I usually have something like
struct NodeStruct {
my_data: i32,
link: Node
}
and struct Node(Rc);
or struct Node(Option>);
if I need cross-links.Great thing is you can then add 'methods' to your type with
impl Node {}
Or define operators and other traits with: impl Add for Node {}
Sometimes, when I need mutability I even wrap the NodeStruct in RefCell.It seems like a lot of wrappers but thanks to them you can have very nice code that uses this type that has pretty much 'normal modren language' semantics + value semantics and is still blazing fast.
When you implement Ord, Eq, Hash they all go through all the wrappers and let you treat your final type Node as a comparable, sortable, hashable and cheaply clonable value. Dereferencing also goes through all or most of the wrappers automatically.