Deref but specifically handles cases where the value
accessed is mutable. Despite its name, DerefMut does NOT allow modifying the
inner value - it only indicates that the container itself is mutable.
Deref but specifically handles cases where the value
accessed is mutable. Despite its name, DerefMut does NOT allow modifying the
inner value - it only indicates that the container itself is mutable.
pub trait DerefMut
#[derive(Copy, Drop)]
struct MutWrapper {
value: T
}
impl MutWrapperDerefMut> of DerefMut> {
type Target = T;
fn deref_mut(ref self: MutWrapper) -> T {
self.value
}
}
// This will work since x is mutable
let mut x = MutWrapper { value: 42 };
let val = x.deref_mut();
assert!(val == 42);
// This would fail to compile since y is not mutable
let y = MutWrapper { value: 42 };
let val = y.deref_mut(); // Compile error
fn deref_mut(ref self: T) -> DerefMutTarget
type Target;
Was this page helpful?