Skip to main content
InterviewHubPRO
Section Overview
Q16

Question 16 of 50

Part 2: Core Architecture & Implementation (Mid-Level)

32%
Rust Systems & Tokio (50 Qs)•Part 2: Core Architecture & Implementation (Mid-Level)

What is Interior Mutability and how do `RefCell<T>` and `Mutex<T>` work?

senior LevelSystem Design

In Simple Words: Allows mutating data even when there is an immutable reference (&T) to that data:

  • RefCell<T> (Single-Threaded): Moves borrow checking from compile-time to runtime:
    • borrow() grants an immutable reference; borrow_mut() grants a mutable reference.
    • If borrow rules are violated at runtime, it panics!
  • Mutex<T> (Multi-Threaded): Provides mutual exclusion across threads. A thread must call lock() to acquire the lock and access the interior data; the lock automatically releases when the MutexGuard goes out of scope.