Cheat Sheets

Author

The MidWit

Ownership Mutability and Operation sheet {own-mut-op-methods}

Table 1: Updated ownership/mutability/operation combinations with compiler results
Ownership Mutability Operation Result
own mutable mutate valid with Copy, but doesn’t mutate the original — mut self consumes a copy1
own mutable transform valid, but mut is unnecessary — the method never writes through self
own immutable mutate no — contradiction (immutable + mutate E0594)
own immutable transform valid, but E0382 if reused without Copy
borrow mutable mutate valid — mutates original in place (test_mutation passes)2
borrow mutable transform valid — creates new Self, original unchanged; &mut self doesn’t change transformation behaviour
borrow immutable mutate no — contradiction (immutable + mutate)
borrow immutable transform valid

Footnotes

  1. This needs Copy/Clone to compile, but won’t cause a persistent change in the original object because it doesn’t actually touch the original object.↩︎

  2. Keep in mind that the return type has to be &mut Self for this to work.↩︎