Why doesn't Rc::into_unwrapped() exist?


#1

Rc::try_unwrap() consumes an Rc and returns the contained value if the Rc had no other strong references. If the Rc did have other strong references, it returns an error result containing the consumed Rc.

Meanwhile, there’s Rc::make_mut(&mut self), which is implemented if T: Clone, and clones the inner value if necessary to get to the no other strong references state.

So by that logic, why not make Rc::into_unwrapped() for T: Clone, and have it always return the contained value, cloning if necessary?


#2

I would think most would just clone. If a programmer sees the performance benefit they can easily write;

Rc::try_unwrap(_).unwrap_or_else(|v|(*v).clone())


#3

Right, so not enough demand yet to add.

Also, I just realized that try_unwrap() only just became available in stable too.