WalzoneInterview Prep
πŸ“ž Interviewing soon? Practice with a realistic AI mock phone interview β€” it calls you, then scores you. First 15 min FREE β†’

Rust Β· Intermediate Β· question 31 of 100

Explain the differences between interior and exterior mutability.?

πŸ“• Buy this interview preparation book: 100 Rust questions & answers β€” PDF + EPUB for $5

In Rust, mutability is at the core of its design philosophy. Mutability refers to the ability to modify a value after it has been created. Rust has two types of mutability - interior mutability and exterior mutability.

1) Interior Mutability: Interior mutability allows us to change the content of a value even if its immutable. In simpler words, we can modify a value through a shared reference without invalidating other references to the same value. Rust provides the β€˜cellβ€˜ and β€˜syncβ€˜ modules to implement interior mutability.

For example, the β€˜Cell<T>β€˜ type uses interior mutability to allow us to mutate a value through a shared reference:

    use std::cell::Cell;
    
    let x = Cell::new(42);
    
    let y = &x;
    let z = &x;
    
    x.set(24);
    
    assert_eq!(x.get(), 24);
    assert_eq!(y.get(), 24);
    assert_eq!(z.get(), 24);

In this example, β€˜xβ€˜ is a β€˜Cell<T>β€˜ that contains the value β€˜42β€˜. We can create multiple shared references to β€˜xβ€˜ by binding β€˜yβ€˜ and β€˜zβ€˜ to it. Then we can use the β€˜set()β€˜ method to mutate the value of β€˜xβ€˜. Note that the immutable shared references β€˜yβ€˜ and β€˜zβ€˜ are not invalidated by the mutation of β€˜xβ€˜.

2) Exterior Mutability: Exterior mutability differs from interior mutability in that it allows us to mutate a value through a mutable reference. Exterior mutability is the traditional form of mutability found in most programming languages. Rust requires us to explicitly mark a value as mutable to mutate its contents.

For example, the following code uses exterior mutability to mutate a value:

    let mut x = 42;
    
    let y = &mut x; // mutable reference here
    
    *y = 13; // mutate x through y
    
    assert_eq!(x, 13);

Here, β€˜xβ€˜ is a mutable integer and is explicitly marked as β€˜mutβ€˜. We create a mutable reference to β€˜xβ€˜ with β€˜let y = &mut xβ€˜. Finally, we mutate β€˜xβ€˜ through the mutable reference β€˜yβ€˜ with β€˜*y = 13β€˜. The value of β€˜xβ€˜ is now β€˜13β€˜.

The key difference between the two is that interior mutability allows runtime borrow checks while exterior mutability only allows compile-time borrows checks. Interior mutability also allows us to modify immutable data types without copying, which can be beneficial for performance reasons. While exterior mutability can make mutation more explicit and easier to reason about.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic Rust interview β€” then scores it.
πŸ“ž Practice Rust β€” free 15 min
πŸ“• Buy this interview preparation book: 100 Rust questions & answers β€” PDF + EPUB for $5

All 100 Rust questions Β· All topics