liveBook · Manning

DerefMut

As explained in Learn Rust in a Month of Lunches

DerefMut is a Rust standard trait that defines mutable dereferencing behavior for a type and requires the type to implement Deref.

DerefMut is a Rust standard trait for mutable dereferencing. It lets code modify the value reached through *value, and it requires Deref first. section 15.3.3

Its implementation follows the immutable Deref implementation:

Making a wrapped number mutable

A HoldsANumber value can implement Deref for read access and DerefMut for write access. With both traits, `*my_number = 30` changes the wrapped `u8`. Without DerefMut, the compiler reports that DerefMut is required to modify through the dereference. section 15.3.2 section 15.3.3

use std::ops::{Deref, DerefMut};
 
struct HoldsANumber(u8);
 
impl HoldsANumber {
    fn prints_the_number_times_two(&self) {
        println!("{}", self.0 * 2);
    }
}
 
impl Deref for HoldsANumber {
    type Target = u8;
 
    fn deref(&self) -> &Self::Target {
        &self.0
    }
}
 
impl DerefMut for HoldsANumber {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.0
    }
}
 
fn main() {
    let mut my_number = HoldsANumber(20);
    *my_number = 30;                               #1
    println!("{:?}", my_number.checked_sub(100));
    my_number.prints_the_number_times_two();
}

Inspect the complete implementation and the assignment through the mutable dereference. section 15.3.3

DerefMut is powerful but should fit the meaning of the type. It works naturally for simple wrapper types such as smart pointers; using it to expose an unrelated field can make operations like -= or push difficult to understand. Named methods may communicate complex behavior more clearly. section 15.3.4 section 15.4

How Learn Rust in a Month of Lunches covers it

How deep it goes
Explains the trait signature, inherited associated type, implementation steps, compiler error, and readability trade-offs.
Examples
Uses a HoldsANumber wrapper, a failed assignment, a corrected implementation, and a Character design warning.
What it assumes
Assumes familiarity with Deref and teaches the inherited Target type and mutable-reference signature directly.
What you can do after
Apply DerefMut to a Deref-enabled wrapper and diagnose missing mutable-dereference implementations from compiler feedback.
How it connects
Connects mutable dereferencing with Deref, associated types, smart pointers, wrapper types, and type safety.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn Rust in a Month of Lunches. The section links point to the parts of the book it draws on.