简体   繁体   English

在rust中,move对作为引用的变量做了什么?

[英]In rust, what does move do for variables that are references?

I have a function that composes (or pipes?) multiple functions into a single one:我有一个 function 将多个函数组合(或管道?)到一个函数中:

fn compose<'a, T>(fns: &'a [&dyn Fn(T) -> T]) -> Box<dyn Fn(T) -> T + 'a> {
  // Why do I need to move fns here?
  // If I don't put move, I get the following error:
  // to force the closure to take ownership of `fns` (and any other referenced variables), use the `move` keyword: `move `
  Box::new(move |mut x| {
    for f in fns {
      x = f(x);
    }
    x
  })
}

I don't get why I need to move the fns into the closure when it's just a reference.我不明白为什么我需要将 fns 移到闭包中,因为它只是一个参考。 What does move actually do here? move 在这里实际上做了什么?

A reference is just a normal variable, like any other variable.引用只是一个普通变量,就像任何其他变量一样。 If you don't move , your fns inside the closure will be a reference to a reference, which goes out of scope at the end of the function.如果你不move ,你在闭包内的fns将是对引用的引用,它在 function 的末尾从 scope 出来。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM