zoukankan      html  css  js  c++  java
  • 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]# 
  • 相关阅读:
    《构建高性能web站点》阅读笔记(三)
    哈希表的C实现(一)
    《大规模web服务开发技术》阅读笔记
    CentOS搭建python开发环境
    Instagram的技术探索(2)
    CentOS5.5编译安装gvim7.3 失败记录
    由于启动用户实例的进程时出错,导致无法生成 SQL Server 的用户实例。该连接将关闭
    wp开发中解决gb2312的编码问题
    WP8 SDK 视图设计器 未将对象设置到对象的实例解决方法
    Oracle误删表的恢复
  • 原文地址:https://www.cnblogs.com/dream397/p/14206932.html
Copyright © 2011-2022 走看看