Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add refcell1 exercise #1779

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

feat: add refcell1 exercise #1779

wants to merge 1 commit into from

Conversation

exdx
Copy link
Contributor

@exdx exdx commented Nov 18, 2023

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.

@shadows-withal
Copy link
Member

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?

@exdx
Copy link
Contributor Author

exdx commented Nov 29, 2023

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
Copy link
Contributor Author

exdx commented Dec 2, 2023

@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.

@mo8it
Copy link
Contributor

mo8it commented Mar 27, 2024

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.

@exdx
Copy link
Contributor Author

exdx commented Mar 28, 2024

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.

@mo8it
Copy link
Contributor

mo8it commented Mar 28, 2024

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

@Piyuuussshhh
Copy link

Piyuuussshhh commented Oct 4, 2024

I think exercises on Cell, RefCell, RwLock and Mutex in sequence would be nice. I remember there was a video that explained when these smart pointers become useful quite nicely. It explains why and when interior mutability is required.
The dude took the construction of a graph and updating the value of a node & its neighbors as the running example throughout the vid. If you want, I could link it!

PS: I'm sorry if exercises on these features have already been discussed and decided. I just finished all exercises and was wondering why these weren't included. BTW doing god's work, you guys! Thank you so much for creating this project!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants