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

Add section about Ownership before References #2468

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

Mandragorian
Copy link
Collaborator

Introduction of references was a bit unmotivated without a brief introduction to Ownership. The read might not understand why they are important in rust.

Introduction of references was a bit unmotivated without a brief
introduction to Ownership. The read might not understand why they are
important in rust.
Comment on lines +31 to +34
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.
Copy link
Collaborator

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.

Copy link
Collaborator Author

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.

@randomPoison
Copy link
Collaborator

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.

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.

3 participants