This crate can be used to mutably borrow the contents of an Rc, if there are no other
strong references, despite any Weak references.
This crate is unsound, as it can result in use-after-free in safe code.
let mut rc = Rc::new("asdf".to_string());
std::mem::forget(Rc::borrow_mut(&mut rc));
drop(rc.clone());
println!("use after free: {rc:?}");Also, this crate requires specific Rust nightly versions. Currently, it targets
nightly-2026-03-01 and may not compile on other versions.
use std::rc::Rc;
use rc_borrow_mut::RcBorrowMut;
let mut rc = Rc::new(0);
let weak = Rc::downgrade(&Rc::clone(&rc));
// Wont panic, since there are no other strong references.
let mut mutable = Rc::borrow_mut(&mut rc);
*mutable += 1;
// Weak references don't work during mutation.
assert!(weak.upgrade().is_none());
*mutable += 1;
drop(mutable);
// Weak references start working again after we are done mutating.
assert_eq!(*weak.upgrade().unwrap(), 2);- Requires
nightlyRust - Uses
unsafe - Depends on unspecified implementation details of
Rc - Not rigorously verified
Licensed under either of
- Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.