std-method

Rust RefMut::map_split

Split a writer guard into guards for distinct parts that Rust can prove do not overlap. The original cell stays exclusively borrowed until both guards are dropped. Use normal safe splitting operations inside the closure. Expected output (not executed): (3, 7) Why: The closure returns two disjoint field references. Common mistake: Trying to produce two mutable references to the same field.

Formal Syntax & Structure

rust syntaxrust
RefMut::map_split(guard, |v| (&mut v.0, &mut v.1))

Working Example

Example Snippetrust
use std::cell::{RefCell, RefMut};fn main() {    let stats = RefCell::new((2, 5));    let (mut a, mut b) = RefMut::map_split(stats.borrow_mut(), |v| (&mut v.0, &mut v.1));    *a += 1; *b += 2;    drop(a); drop(b);    println!("{:?}", *stats.borrow());}
Rust Reference