Trying to learn rust still, and currently going through Introduction - Learning Rust With Entirely Too Many Linked Lists and on the persistent list.
Code in a Playground link
In this, when
prepend is called, it clones the head. This calls
Option, which will call
clone on the
Rc<Node<T>> which ups the reference count (which is when I learnt std uses unsafe blocks in it's implementation).
But then I assume the
drop is called at some point as there's only one reference to that list at the prepend right? I'd love to inject a "
Rc::drop or similar to try and work out the flow.
I don't think you can realistically monkey patch methods in Rust. The only options I can think of are:
- Do it statically: fork
std, modify it, re-compile it locally. Modifying
std and trying to use a local build is going to be painful, as
std requires nightly and AFAICT there are other, additional complications involved as well.
- Do it dynamically, e.g. attach a debugger and load an external dylib. This is probably very complicated, and surely not reliable, since the
Rc::drop impls may well have been inlined.
That's an easy workaround, albeit partial – it won't affect other types that already contain
Thanks - in this case that works for me, and allowed me to play. e.g. if you break the
let list = list.prepend(1).prepend(2).prepend(3);
into 3 lines then nothing get's dropped to the end.