rust - When does a closure implement Fn, FnMut and FnOnce? -


what specific conditions closure implement fn, fnmut , fnonce traits?

that is:

  • when closure not implement fnonce trait?
  • when closure not implement fnmut trait?
  • when closure not implement fn trait?

for instance, mutating state of closure on it's body makes compiler not implement fn on it.

the traits each represent more , more restrictive properties closures/functions, indicated signatures of call_... method, , particularly type of self:

  • fnonce (self) functions can called once,
  • fnmut (&mut self) functions can called if have &mut access environment
  • fn (&self) functions can still called if have & access environment.

a closure |...| ... automatically implement many of can.

  • all closures implement fnonce: closure can't called once doesn't deserve name. note if closure implements fnonce, can called once.
  • closures don't move out of captures implement fnmut, allowing them called more once (if there unaliased access function object).
  • closures don't need unique/mutable access captures implement fn, allowing them called everywhere.

these restrictions follow directly type of self , "desugaring" of closures structs (described in finding closure in rust).


Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -