std::rc::Rc::new_cyclic with errors

I'm writing code that wants to set up a data structure representing a graph of data which can be cyclic. The data corresponds to types in a type system, but that's not particularly relevant. Each node in the graph holds only weak references to other nodes, and then all the Rcs are stored in a vec somewhere. This is all fine and good except that there are cases where errors can occur during processing. It'd be great to have a way to return an error and leave that Weak dangling.

I guess the argument against it would be that now you have this uninitialized Weak, but what else is there to do? The only other recourse I can see is to have interior mutability for the references and to fill them in later, but that feels strictly worse.

It'd also be fine if there were a form that returned an Option and then I could close over an error.

Anyway, the question here is, would this make sense as an issue for a feature request for std::rc::Rc?

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.