Cyclic data structures

Now that cyclic is merged (available in 1.60).

How can I create recursive tree like structures? For example here

Are they actually useful in creating such data structures?

You can use them to build a tree like that provided you know the full structure of the entire tree up front. (If you don't know the full structure, then your tree is mutable and you need a RefCell or similar either way, in which case new_cyclic is not necessary to build these structures in the first place.)

Yes. I tried it and realised that I need RefCell, then cyclic is of no use. Are there any other use cases for new_cyclic?

The reason that new_cyclic exists is that without it, you cannot create an immutable data structure with cyclic arcs unless you use the unsafe get_mut_unchecked method.

It does not really exist to address use-cases where you are able to mutate the contents of the Arc safely, though it can be convenient in those cases too.

8 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.