google/comprehensive-rust

Fn traits slide is more confusing than necessary

fw-immunant opened this issue · 0 comments

The example code demonstrates several things at once:

  • the applicable Fn* trait being dependent on how a closure interacts with the environment
  • a Fn closure that closes over nothing (which is trivially Copy) being Copy
  • the sub/supertrait relation that allows all of these to be passed to apply_with_log (which should probably be called apply_and_log instead)

Because the first closure (add_3) does not use the environment at all (even immutably), it doesn't clearly show the symmetry of environment use and the corresponding Fn* trait method receiver. Currently add_3 is passed by value (not shared reference) to apply_with_log, which works because it is Copy, but does not perfectly correspond to the &self receiver of Fn::call. I think we should have all three closures access the environment (immutably, mutably, and by move) so as to make it as clear as possible what's happening here. We might also separately call out that closures capturing nothing, as well as functions, implement the Fn trait, and that closures implement Copy and Clone whenever possible, but these are all already mentioned in the speaker notes.