How can a closure using the `move` keyword create a FnMut closure?

前端 未结 2 868
陌清茗
陌清茗 2021-02-06 14:57

Up to this moment I thought that move |...| {...} would move variables inside a closure and the closure would implement only FnOnce, because you can mo

相关标签:
2条回答
  • 2021-02-06 15:54

    Up to this moment I thought that move |...| {...} will move variables inside closure and closure will implement only FnOnce, because you can move vars only once.

    The variables are moved when the closure is created, not when it is invoked. Since you're only creating one closure, the move only happens once - regardless of how often map calls the function.

    0 讨论(0)
  • 2021-02-06 15:59

    Yes, this point is quite confusing, and I think the wording of the Rust book contributes. After I read it, I thought the same as you did: that a move closure was necessarily FnOnce, and that a non-move closure was FnMut (and may also be Fn). But this is kind-of backwards from the real situation.

    The closure can capture values from the scope where it's created. move controls how those values go into the closure: either by being moved, or by reference. But it's how they're used after they're captured that determines whether the closure is FnMut or not.

    If the body of the closure consumes any value it captured, then the closure can only be FnOnce. After the closure runs the first time, and consumes that value, it can't run again.

    As you've mentioned, you can consume a value inside the closure by calling drop on it, or in other ways, but the most common case is to return it from the closure, which moves it out of the closure. Here's the simplest example:

    let s = String::from("hello world");
    let my_fnonce = move || { s };
    

    If the body of the closure doesn't consume any of its captures, then it's FnMut, whether it was move or not. If it also doesn't mutate any of its captures, it's also Fn; any closure that is Fn is also FnMut. Here's a simple example, albeit not a very good one.

    let s = "hello world";
    let my_fn = move || { s.length() }
    

    Summary

    The move modifier controls how captures are moved into the closure when it's created. FnMut membership is determined by how captures are moved out of the closure (or consumed in some other way) when it's executed.

    0 讨论(0)
提交回复
热议问题