std::ops::DerefMut

Trait std::ops::DerefMut

#[lang = "deref_mut"]
pub trait DerefMut: Deref {
    fn deref_mut(&mut self) -> &mut Self::Target;
}

The DerefMut trait is used to specify the functionality of dereferencing mutably like *v = 1;

DerefMut also enables 'Deref coercions'.

Examples

A struct with a single field which is modifiable via dereferencing the struct.

use std::ops::{Deref, DerefMut};

struct DerefMutExample<T> {
    value: T
}

impl<T> Deref for DerefMutExample<T> {
    type Target = T;

    fn deref(&self) -> &T {
        &self.value
    }
}

impl<T> DerefMut for DerefMutExample<T> {
    fn deref_mut(&mut self) -> &mut T {
        &mut self.value
    }
}

fn main() {
    let mut x = DerefMutExample { value: 'a' };
    *x = 'b';
    assert_eq!('b', *x);
}

Required Methods

The method called to mutably dereference a value

Implementors

© 2010 The Rust Project Developers
Licensed under the Apache License, Version 2.0 or the MIT license, at your option.
https://doc.rust-lang.org/std/ops/trait.DerefMut.html

在线笔记
App下载
App下载

扫描二维码

下载编程狮App

公众号
微信公众号

编程狮公众号

意见反馈
返回顶部