rustlings icon indicating copy to clipboard operation
rustlings copied to clipboard

feat: add refcell1 exercise

Open exdx opened this issue 2 years ago • 7 comments

Adds a basic exercise to introduce the concept of a RefCell. This API is not necessarily for beginners but is useful to have knowledge of.

exdx avatar Nov 18 '23 22:11 exdx

I like the idea of teaching about RefCells, but I feel like this exercise would have to be somewhat more sophisticated... I'd like people to be able to see the difference of behaviour between the normal compile-time borrow check and what RefCell provides, maybe a more engaging exercise format could be converting something that's impossible with normal borrowing rules to using RefCell?

manyinsects avatar Nov 29 '23 16:11 manyinsects

Thanks for taking a look @shadows-withal. I agree the example could be more sophisticated and I will look into updating it. Potentially following the Rust Book more closely is a good direction to take.

exdx avatar Nov 29 '23 17:11 exdx

@shadows-withal I have updated the example to use the compile instead of test mode. I think this make the logic considerably simpler. The example is a straightforward example of using a RefCell to mutate via an immutable reference.

I think since this may be the first introduction to RefCell a user has, it's simplicity is a feature. Happy to adjust as necessary.

exdx avatar Dec 02 '23 19:12 exdx

The exercise teaches the usage of RefCell very well, but not really when to use it. It might be just confusing why the user shouldn't use &mut self since a RefCell is not really required in this example at all.

mo8it avatar Mar 27 '24 17:03 mo8it

The exercise teaches the usage of RefCell very well, but not really when to use it. It might be just confusing why the user shouldn't use &mut self since a RefCell is not really required in this example at all.

I see -- that's fair. At the risk of making the example more convoluted I can update the example to provide a scenario where refcell is a necessity.

exdx avatar Mar 28 '24 16:03 exdx

I am a very new maintainer. Let's wait for the feedback from @shadows-withal first :D

mo8it avatar Mar 28 '24 16:03 mo8it