std-method

Rust Ref::map

Project a read guard to a component without dropping the underlying runtime borrow. This helps expose a narrow view from an API while retaining the necessary guard. Expected output (not executed): 40 true Why: Projection narrows the visible component, not the cell-wide access check. Common mistake: Assuming the other field becomes freely mutable through a new borrow.

Formal Syntax & Structure

rust syntaxrust
Ref::map(guard, |value| &value.field)

Working Example

Example Snippetrust
use std::cell::{Ref, RefCell};fn main() {    let account = RefCell::new(("A17", 40));    let balance = Ref::map(account.borrow(), |a| &a.1);    println!("{}", *balance);    println!("{}", account.try_borrow_mut().is_err());}
Rust Reference