std::ops::FnMut

Trait std::ops::FnMut

#[lang = "fn_mut"]
pub trait FnMut<Args>: FnOnce<Args> {
    extern "rust-call" fn call_mut(&mut self, args: Args) -> Self::Output;
}

A version of the call operator that takes a mutable receiver.

Examples

Closures that mutably capture variables automatically implement this trait, which allows them to be invoked.

let mut x = 5;
{
    let mut square_x = || x *= x;
    square_x();
}
assert_eq!(x, 25);

Closures can also be passed to higher-level functions through a FnMut parameter (or a FnOnce parameter, which is a supertrait of FnMut).

fn do_twice<F>(mut func: F)
    where F: FnMut()
{
    func();
    func();
}

let mut x: usize = 1;
{
    let add_two_to_x = || x += 2;
    do_twice(add_two_to_x);
}

assert_eq!(x, 5);

Required Methods

???? This is a nightly-only experimental API. (fn_traits #29625)

This is called when the call operator is used.

Implementors

  • impl<'a, A, F> FnMut<A> for &'a F where
        F: Fn<A> + ?Sized
  • impl<'a, A, F> FnMut<A> for &'a mut F where
        F: FnMut<A> + ?Sized

© 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.FnMut.html

在线笔记
App下载
App下载

扫描二维码

下载编程狮App

公众号
微信公众号

编程狮公众号

意见反馈
返回顶部