• rust FnMut 闭包


    fn consume_with_relish<F>(mut func: F) where F: FnMut() -> String
    {
        // `func` consumes its captured variables, so it cannot be run more
        // than once
        println!("Consumed 1 ");
        func();
        println!("Consumed 2");
        func();
    
        println!("Delicious!");
    
        // Attempting to invoke `func()` again will throw a `use of moved
        // value` error for `func`
    }
    fn main() {
            let mut  x = String::from("x");
        let consume_and_return_x = move || {
                            x.push('*');
                            println!("x {}", x);
            };
        consume_with_relish(consume_and_return_x);
    
    // `consume_and_return_x` can no longer be invoked at this point
    }
    cargo build
       Compiling hello_world v0.1.0 (/data2/rust/FnMut)
    error[E0271]: type mismatch resolving `<[closure@src/main.rs:17:32: 20:3 x:_] as std::ops::FnOnce<()>>::Output == std::string::String`
      --> src/main.rs:21:5
       |
    1  | fn consume_with_relish<F>(mut func: F) where F: FnMut() -> String
       |                                                            ------ required by this bound in `consume_with_relish`
    ...
    21 |     consume_with_relish(consume_and_return_x);
       |     ^^^^^^^^^^^^^^^^^^^ expected struct `std::string::String`, found `()`
    
    error: aborting due to previous error
    
    For more information about this error, try `rustc --explain E0271`.
    error: could not compile `hello_world`.
    
    To learn more, run the command again with --verbose.

    原来 FnMut 没返回值

     
    pub trait FnMut<Args>: FnOnce<Args> {
        extern "rust-call" fn call_mut(&mut self, args: Args) -> Self::Output;
    }
    [root@bogon FnMut]# cargo build
       Compiling hello_world v0.1.0 (/data2/rust/FnMut)
        Finished dev [unoptimized + debuginfo] target(s) in 0.43s
    [root@bogon FnMut]# cargo run
        Finished dev [unoptimized + debuginfo] target(s) in 0.01s
         Running `target/debug/hello_world`
    Consumed 1 
    x x*
    Consumed 2
    x x**
    Delicious!
    [root@bogon FnMut]# cat src/main.rs 
    fn consume_with_relish<F>(mut func: F) where F: FnMut()
    {
        // `func` consumes its captured variables, so it cannot be run more
        // than once
        println!("Consumed 1 ");
        func();
        println!("Consumed 2");
        func();
    
        println!("Delicious!");
    
        // Attempting to invoke `func()` again will throw a `use of moved
        // value` error for `func`
    }
    fn main() {
            let mut  x = String::from("x");
        let consume_and_return_x = move || {
                            x.push('*');
                            println!("x {}", x);
            };
        consume_with_relish(consume_and_return_x);
    
    // `consume_and_return_x` can no longer be invoked at this point
    }
    [root@bogon FnMut]# 
  • 相关阅读:
    Open diary(每天更新)
    独立博客与秘密基地,以及对UI设计中拟物态的怀念
    如何在宝塔面板里设置脚本定时恢复数据库
    LaTex公式语法教程及手册(附emlogpro公式显示插件katex说明)
    关于梦的冷知识
    PTui又加全景图 佳田未来城 of 安阳
    原生js 以ajax(post)的方式传json至php,并让php解析为数组
    Python面向对象——
    Python中的模块、常用模块——12
    Python中的装饰器——11
  • 原文地址:https://www.cnblogs.com/dream397/p/14206932.html
Copyright © 2020-2023  润新知