小编use*_*425的帖子

为什么要调用FnOnce闭包?

我试图将一个闭包传递给一个函数,然后该函数将在函数范围内改变传递给它的东西.根据我目前对Rust的理解,这应该是这样的:

pub fn call_something(callback: &FnOnce(&mut Vec<i32>)) {
    let mut my_vec = vec![0, 1, 2, 3, 4];
    callback(&mut my_vec);
}
Run Code Online (Sandbox Code Playgroud)

这导致了这些错误:

error[E0161]: cannot move a value of type dyn for<'r> std::ops::FnOnce(&'r mut std::vec::Vec<i32>): the size of dyn for<'r> std::ops::FnOnce(&'r mut std::vec::Vec<i32>) cannot be statically determined
 --> src/lib.rs:3:5
  |
3 |     callback(&mut my_vec);
  |     ^^^^^^^^

error[E0507]: cannot move out of borrowed content
 --> src/lib.rs:3:5
  |
3 |     callback(&mut my_vec);
  |     ^^^^^^^^ cannot move out of borrowed content
Run Code Online (Sandbox Code Playgroud)

为什么要FnOnce采取行动?我在这里错过了什么?

closures move-semantics rust

7
推荐指数
1
解决办法
200
查看次数

标签 统计

closures ×1

move-semantics ×1

rust ×1