# Rc and internal mutability

**URL:** <https://internals.rust-lang.org/t/rc-and-internal-mutability/11463>\
**Category:** libs\
**Created:** [December 10, 2019, 5:56pm UTC](https://internals.rust-lang.org/t/rc-and-internal-mutability/11463 "2019-12-10T17:56:07Z")\
**Posts on this page:** 1\
**Showing post:** 13

<div class="post-metadata">

**Author:** ![xfix](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/xfix/32/2726_2.png) [@xfix](https://internals.rust-lang.org/u/xfix)\
**Post date:** [December 15, 2019, 12:22am UTC](https://internals.rust-lang.org/t/rc-and-internal-mutability/11463/13 "2019-12-15T00:22:01Z")

</div>

`Rc::get_mut_unchecked` probably should require no other `Rc` and `Weak` pointers to the same value. It's possible to cause unsafety without violating "Any other `Rc` or `Weak` pointers to the same value must not be dereferenced for the duration of the returned borrow" requirement.

```rust
#![feature(get_mut_unchecked)]

use std::rc::Rc;

unsafe fn f<'a>(mut r: Rc<&'a str>, s: &'a str) {
    *Rc::get_mut_unchecked(&mut r) = s;
}

fn main() {
    let x = Rc::new("Hello, world!");
    {
        let s = String::from("Replaced");
        unsafe { f(Rc::clone(&x), &s) };
    }
    println!("{}", x);
}

```

`Rc::into_raw` probably should return a pointer without involving deref however (similarly to how `Weak` already does it in its `as_raw` method).

I don't think `Rc` should be modified to use `UnsafeCell`, as this changes variance, when the current variance is fine, and this is not necessary when `Rc::into_raw` could be changed instead.

---

_[View the full topic](https://internals.rust-lang.org/t/rc-and-internal-mutability/11463)._
