std::convert::AsMut

Trait std::convert::AsMut

pub trait AsMut<T> where    T: ?Sized, {
    fn as_mut(&mut self) -> &mut T;
}

A cheap, mutable reference-to-mutable reference conversion.

This trait is similar to AsRef but used for converting between mutable references.

Note: this trait must not fail. If the conversion can fail, use a dedicated method which returns an Option<T> or a Result<T, E>.

Generic Implementations

  • AsMut auto-dereferences if the inner type is a reference or a mutable reference (e.g.: foo.as_ref() will work the same if foo has type &mut Foo or &&mut Foo)

Examples

Box<T> implements AsMut<T>:

fn add_one<T: AsMut<u64>>(num: &mut T) {
    *num.as_mut() += 1;
}

let mut boxed_num = Box::new(0);
add_one(&mut boxed_num);
assert_eq!(*boxed_num, 1);

Required Methods

Performs the conversion.

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/convert/trait.AsMut.html

在线笔记
App下载
App下载

扫描二维码

下载编程狮App

公众号
微信公众号

编程狮公众号

意见反馈
返回顶部