7 ms·
For this example id probably accumulate the score total in a local variable. Then once iterating over all the children i would call parent.add_score() with the
by phibz 11mo ago
For this example id probably accumulate the score total in a local variable. Then once iterating over all the children i would call parent.add_score() with the accumulated total
- qouteall 11mo agoI added clarification (That simplified example is just for illustrating contagious borrow issue. The *`total_score` is analogous to a complex state that exists in real applications*. Same for subsequent examples. Just summing integer can use `.sum()` or local variable. Simple integer mutable state can be workarounded using `Cell`.)