[RUST - High Scores] Explain multiple approaches using the High Scores exercises

I think that the exercise High Scores gives us a good opportunity to explain different trade-offs for different approaches. I can think of three different approaches here in terms of the burden of calculating the top 3 values:

  1. Calculate them on the new method and store them as attributes.
  2. Lazily calculate them on the first call for either personal_best or personal_top_three methods, caching them after that. Here, we have a good opportunity to explain the interior mutability pattern, given that those methods immutably borrow the self and we cannot simply turn them into mutable references.
  3. Calculate them on each call for either personal_best or personal_top_three methods, which does not give us the best performance.

I you want, I can create the 3 approaches and add them in such a way that they will appear here:
Image

2 Likes

I like this idea, feel free to go ahead! I look forward to your PR :smile:

Indeed, std::cell::OnceCell could be a good fit for this. Or just Cell to keep it simple, or an entirely different method, I’ll leave the decision up to you.

This could be a good explanation of the concept of memoization (though the “memoization” crate is too much for this exercise)

1 Like