FnOnce

Fn FnMut FnOnce以及move的区别

拜拜、爱过 提交于 2019-12-10 05:58:21
There are three different "kinds" of closure in Rust, Fn, FnMut, and FnOnce, these differ in that their calling methods take &self, &mut self, and self respectively. This means that Fn closures have immutable access to their captures, FnMut get mutable access, and FnOnce can move out of their environment. As a result, Fns can be called through an & reference, FnMuts require an &mut reference, and FnOnce requires ownership. Since Iron calls Handlers with only an & reference, only Fn's can be used as Handlers. Sometimes Rust can infer the kind of a closure from its usage site, but in many cases