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.less
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.ide
Sometimes Rust can infer the kind of a closure from its usage site, but in many cases you must annotate it - in this case with |&: args: Types|, the annotation for an Fn. The annotations for FnMut and FnOnce are &mut: and : respectively.ui
[–]GolDDranks[S] 1 point 9 months agothis
I'm a bit confused – is using "move" keyword orthogonal to closure being Fn, FnMut or FnOnce? If I understood right, the latter is decided by the using this kind of annotation: |&:| for Fn, |&mut:| for FnMut and |:| for FnOnce. Then, what does move || correspond with? There seems to be two cases where a captured value is moved, copied or borrowed: when a closure is made (creating the struct with function pointer and the relevant environment) and when a closure is called. Did I understand right: move keyword means that a value is moved into the struct when a closure is CREATED. And Fn, FnMut and FnOnce decide how a value is moved/borrowed from the struct to the function body when a closure is CALLED?three
[–]jonreemhyper · iron · stainless 1 point 9 months agoip
That's exactly right - they are orthogonal. move || is just asking rustc to attempt to infer the kind of closure, referring to Fn/FnMut/FnOnce.ci