DerefMut

The DerefMut trait does the same thing Deref does, but it is used when derefencing a mutable value. The compiler decides whether to use Deref or DerefMut, so usually when we need to implement one, we need to implement them both.

Here we have an implementation of DerefMut:

pub struct DerefExample {
val: u32,
}

impl DerefMut for DerefExample {
fn deref_mut(&mut self) -> &mut u32 {
return &mut self.val;
}
}

The DerefMut trait requires that the Deref trait is also implemented, and that the deref and deref_mut functions have the same return type.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
52.15.245.1