canonical / rust-best-practices

A set of guidelines to avoid arguments over code-quality
GNU General Public License v3.0
37 stars 0 forks source link

Feedback: scoped-mutability pattern #4

Open CAT-Solstice opened 2 days ago

CAT-Solstice commented 2 days ago

Hello, I noticed this while reading through. The names are not consistent.

Avoid loosely-scoped let mut

[...]

let my_structure = {
   let mut my_structure = MyStructure{}
   // Mutate `my_structure` as required to construct it.
   my_structure
};

For greatest clarity, make sure the name of the outer (immutable) and inner (mutable) declarations have the same name, here my_structure`.

then

Shadowing

[...] In the second case when shadowing in the same scope with the same type, there is no restriction placed on this and it may be done as many times as necessary. However, if this is being used to effectively mutate a value during construction with no other values being affected, instead use the scoped-mutability pattern—

let thing = {
   let mut my_thing = ...;
   // Mutate `my_thing` to construct it...
   my_thing
};

Regards,

TheSignPainter98 commented 1 day ago

Aha good spot! Thank you I'll get that fixed