美文网首页
Rust 闭包学习 (Fn/FnMut/FnOnce)

Rust 闭包学习 (Fn/FnMut/FnOnce)

作者: 普通上班族老王 | 来源:发表于2022-03-25 14:16 被阅读0次

学习 Rust 闭包记录

闭包作为参数

fn main() {
    parameter_is_callback_t(Box::new(|| {
        println!("回调 parameter_is_callback_t");
    }));
}

// 闭包作为参数
fn parameter_is_callback_t<T>(callback: Box<T>)
where
    T: FnOnce() -> (),
{
    println!("parameter_is_callback_t start");
    callback();
    println!("parameter_is_callback_t end");
}

闭包作为结构体属性

fn main() {
    let mut t = FnTest {
        callback: Box::new(callback_function), // 可以直接传入一个 function
        callback_once: Box::new(|v| {
            // 可以直接编写闭包
            println!("FnOnce: {}", v);
        }),
    };

    (t.callback)(1);
}

struct FnTest {
    callback: Box<dyn FnOnce(u8) -> ()>,
    callback_once: Box<dyn FnOnce(u8) -> ()>,
}

fn callback_function(v: u8) {
    println!("Fn callback_function: {}", v);
}

异步使用闭包

主要就是加 Send trait,没加 Send 会报如下错误

    error[E0277]: `dyn FnOnce(u8)` cannot be sent between threads safely
--> src\main.rs:29:5
    |
29  |     thread::spawn(move || {
    |     ^^^^^^^^^^^^^ `dyn FnOnce(u8)` cannot be sent between threads safely
    |
    = help: the trait `Send` is not implemented for `dyn FnOnce(u8)`
    = note: required because of the requirements on the impl of `Send` for `Unique<dyn FnOnce(u8)>`
    = note: required because it appears within the type `Box<dyn FnOnce(u8)>`
    = note: required because it appears within the type `[closure@src\main.rs:29:19: 33:6]`
note: required by a bound in `spawn`
xxx
    |
621 |     F: Send + 'static,
    |        ^^^^ required by this bound in `spawn`
fn main() {
    parameter_is_callback_thread(Box::new(|| {
        println!("回调 parameter_is_callback_thread");
    }));

    let mut thread_t = FnThreadTest {
        callback_once: Box::new(|v| {
            println!("Thread FnOnce: {}", v);
        }),
    };

    thread::spawn(move || {
        println!("thread start");
        (thread_t.callback_once)(1);
        println!("thread end");
    });

    // 等待1秒,不然程序结束了,上面的 thread::spawn 还没运行完
    sleep(Duration::from_secs(1));
}

// 闭包作为参数,并且异步调用
fn parameter_is_callback_thread<T>(callback: Box<T>)
where
    T: FnOnce() -> () + Send + 'static,
{
    println!("parameter_is_callback_thread start");
    thread::spawn(move || {
        println!("parameter_is_callback_thread thread start");
        callback();
        println!("parameter_is_callback_thread thread end");
    });
    println!("parameter_is_callback_thread end");
}

struct FnThreadTest {
    callback_once: Box<dyn FnOnce(u8) -> () + Send>,
}

项目地址

github 项目地址

相关文章

  • Rust 闭包学习 (Fn/FnMut/FnOnce)

    学习 Rust 闭包记录 闭包作为参数 闭包作为结构体属性 异步使用闭包 主要就是加 Send trait,没加 ...

  • rust--匿名函数FnOnce/FnMut/Fn

  • 2022-07-22

    1. rust 闭包 三种 Fn 的关系[https://course.rs/advance/functional...

  • rust 闭包与同步

    rust 闭包与同步 rust 闭包 rust闭包中主要包括两个部分,闭包参数和闭包环境变量。闭包函数和函数参数使...

  • Rust 闭包初探

    Rust 中的闭包 Rust 中的闭包(closure)是一类特殊的函数。与普通函数相比,闭包是匿名的(当然你可以...

  • JavaScript 闭包、定时器

    1.什么是闭包? 有什么作用? 闭包就是能够读取其他函数内部变量的函数。举例: 上述代码中fn4就是闭包,通过fn...

  • JavaScript 闭包

    闭包 首先来个例子 上面代码1中,函数fn1的返回值就是函数fn2, fn2可以读取fn1的内部变量,闭包就是函数...

  • Rust中的闭包:更快更安全

    引子 Rust对函数式编程有着非常良好的支持,从闭包这一特性就能看出来,它不仅实现了经典的功能和语义,还派生出Fn...

  • RUST 学习日记 第20课 ——闭包

    RUST 学习日记 第20课 ——闭包 0x00 回顾与开篇 我们已经通过两节课介绍了Rust的函数使用方法,尤...

  • 闭包

    红宝书上对于闭包的定义:闭包是指有权访问另外一个函数作用域中的变量的函数。 其中 fn 就是一个闭包,在函数 f ...

网友评论

      本文标题:Rust 闭包学习 (Fn/FnMut/FnOnce)

      本文链接:https://www.haomeiwen.com/subject/qdfdjrtx.html