-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
Add section about Ownership before References #2468
base: main
Are you sure you want to change the base?
Conversation
Introduction of references was a bit unmotivated without a brief introduction to Ownership. The read might not understand why they are important in rust.
And this is because of Ownership: Initially `s` is the owner of the `String` we | ||
created. With `let _y = s;`, we moved the ownership of the `String` from `s` to | ||
`_y`. Then, when we try to use `s`, rust won't let us, since it doesn't own a | ||
value to be used any more. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Most of this slide is disclaimer, with this being the key explanatory bit. And, it uses "owner" and "moved" in an undefined way! At this point, we don't actually need to talk about moving or dropping, two things that are closely related to ownership.
In a chat conversation, Dmytro suggested describing references as a way to not copy data, and that seems like a simpler way to introduce this, particularly since we just covered tuples and arrays. Perhaps an update to the "shared references" slide would do better:
- Use a large value like
[0u8; 1024]
as an argument to a function - Demonstrate that dangling references are forbidden by returning
&[0u8;1024]
referencing a local variable
The next slide, on exclusive references, can then do a similar thing but use the passed array as an output buffer. And then the next slide could do a similar thing with &mut[u8]
.
Finally, the slide on strings currently mentions "owned", but that could be softened to just a non-copyable type, because Rust doesn't want to automatically copy what might be very large values.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well, any definition will have to rely on other "undefined" terms. Even in the book, the definition of ownership is that (among others) "every value has an owner" without defining what that "owner" means. I could say that the owner "changed" instead of "moved" to not introduce a lot of terminology.
I don't believe we need to talk about moving concretely. But I think it is good to make a small reference to the fact that something like this happens. This way it can be in the back of the mind of participants, and it could help to process the course from that point of view.
I feel like describing references as a way to avoid copying might lead to confusion. Not only because in reality it is not just that, but also it might lead participants to think that "moving = copying", which is not always true.
At the same time I agree that maybe the shared references slide could do with a better example with a function call, as it feels like a more natural use of references.
I'm not in favor of this change, personally. I don't think it'd be helpful to introduce ownership here. I think we can pretty easily talk about references without needing to mention ownership. Usually I frame it in terms of pointers/references in other languages, since students are already going to be familiar with the concept of a reference (though with different semantics depending on what languages they know). I also think @djmitche's suggestion to frame it in terms of "pass data around without copying" is a good idea, since it provides some motivation for references while not needing to bring up ownership. We also have an example of showing how a dangling reference can't be created/returned, which I think is a good way to hint at ownership without having to fully introduce it. |
Introduction of references was a bit unmotivated without a brief introduction to Ownership. The read might not understand why they are important in rust.