Can the compiler emit trace or debug about when drop is called?

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 clone on 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 Rc 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 "println!" into Rc::drop or similar to try and work out the flow.

You can newtype it.

1 Like

I don't think you can realistically monkey patch methods in Rust. The only options I can think of are:

  1. 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.
  2. 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.
1 Like

That's an easy workaround, albeit partial – it won't affect other types that already contain Rc verbatim.

1 Like

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.