-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Guide Redux: Pointers #15789
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
Guide Redux: Pointers #15789
Conversation
^ | ||
``` | ||
|
||
We can **dereference** the pointer by using the `*` operator. This will work: |
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.
dereference sounds scary 😨
What if you introduce the concept first, like: We have to make rust follow the pointer and fetch the value at that memory address. We can do it by using the * operator. This is called dereferencing, btw.
This is of course just a suggestion. Great work btw, I'm eagerly following each released chapter of the guides 😄
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.
I think the word "dereference" is fine. It means the same thing in Rust that it does in every other language with pointers/references. And I like the bolding of the word, it highlights the fact that this is a potentially unfamiliar but important term that's being introduced for the first time.
I do agree though that this might use some elaboration in case the reader is not in fact familiar with pointers/references already.
We can dereference the pointer by using the
*
operator. Dereferencing a pointer means accessing the value at the location stored in the pointer. This will work:
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.
Yeah, probably how the concept is explained might be a matter of personal taste. I think your suggestion would also do it just fine. I'm glad I'm not the only one who thinks the word dereference calls for more clarification. 😄
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.
Agreed. Updated.
👍 This is brilliant! I'm excited to read the rest of the pointer guide. I agree with Pascal that it would be nice to link to a explanation of Stack vs Heap there. It's probably out of the scope to describe such concepts here but how about just linking to this: http://stackoverflow.com/questions/79923/what-and-where-are-the-stack-and-heap |
fn main() { | ||
let origin = &Point { x: 0.0, y: 0.0 }; | ||
let p1 = box Point { x: 5.0, y: 3.0 }; | ||
Use references when you want to use a pointer, but do not want to take ownership. |
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.
Here "best practices" discusses ownership as an important concept but I don't think it's ever mentioned above in the current version. Is it safe to assume a reader will have seen that in a previous guide before reaching this point? Should this at least reference an in depth discussion of what ownership means and why it matters in Rust?
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.
Good call. Eventually, the 'lifetimes' guide will be the 'ownership' guide, so maybe a link here is warranted.
👍 on the link with explanations about heaps and stacks. The tutorial lost me when it mentioned heaps and stacks. Wikipedia wasn't very helpful. Thank you for that link @cburgdorf. |
-------- ----- | ||
0xd3e010 5 | ||
0xd3e030 8 | ||
``` |
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.
Would it make sense to add a "name" column?
Can you use markdown tables? (I'm not sure which markdown extensions are supported by rustdoc or whatever is rendering this in the end. I'm assuming the Github-flavored version here.) This would probably look better then "code". And you just need to add some pipe symbols.
location | value
-------- | -----
0xd3e010 | 5
0xd3e030 | 8
location | value |
---|---|
0xd3e010 | 5 |
0xd3e030 | 8 |
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.
Yeah, since Markdown has no tables, I have no idea.
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, you could always use HTML… but especially tables are really ugly.
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.
@huonw do you know if rustdoc has any kind of table support?
It would generally be useful to have a cheat sheet showing pointer types and their typical usage with cell types. Something like:
The above is pretty basic. The purpose descriptions in particular need some work. The guide also needs to decide what names to use. Should we refer to By the way, I'm not sure I like the term owned pointer, because "owned" refers to the pointer itself, whereas the the key concept is the pointer owning the data it points to. At least that's how I interpret the name. |
@bachm, I really like this idea. |
I added the cheat sheet, and modified it a bit |
Okay. This now has significant upgrades, and is at least as far along as the current pointer guide. I'd like to stop work here, get this reviewed, and then merge it. The other sections can come later. |
Oh, with the exception of the psudocode issue. That needs sorted before a merge. |
|
||
This part is coming soon. | ||
|
||
# Cheat Sheet<a id="cheat-sheet" href="#cheat-sheet"> |
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.
This link should probably be closed, right?
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.
Doesn't need to be.
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.
Ok, I don't know how the Rust docu is compiled. Just my local markdown parser couldn't handle this.
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.
Ahh weird. A non-closed <a>
is perfectly legal.
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.
FWIW, you don't need an explicit link here, rustdoc automatically inserts ids in this format (and makes the headings links to themselves). E.g. http://doc.rust-lang.org/master/guide.html#ownership-and-lifetimes
I would say that Cell and RefCell are containers, not pointers. (They are not indirections, just encapsulations). Of course they are rather special containers, so they should be treated on their own. Maybe it is wise to separate basic pointer types and smart pointer types in two categories / headings for the table, just to not have readers freak out about the amount of pointers. |
The line gets very blurry. I consider anything that implements |
Absolutely, but Cell and RefCell don't implement Deref/Mut at all. Smart pointers are Rc, Arc, Gc, etc. RefCell has associated handles that are smart pointers -- |
Okay, addressed everything else so far. |
This is super, super WIP, but I'm going to go get lunch for a while, and figured I'd toss my work up here in case anyone wants to see my work as I do it. This contains a new introductory section explaining the basics of pointers, and some pitfalls that Rust attempts to solve. I'd be interested in hearing how my explanation is, as well as if this belongs here. Pointers are such a crucial concept, I don't mind having a beginners' section on them in the main docs, even though our main audience is supposed to understand them already. Reasonable people may disagree, however.
Store binding mode for each instance of a binding independently fix rust-lang#15787
This is super, super WIP, but I'm going to go get lunch for a while, and figured I'd toss my work up here in case anyone wants to see my work as I do it.
This contains a new introductory section explaining the basics of pointers, and some pitfalls that Rust attempts to solve. I'd be interested in hearing how my explanation is, as well as if this belongs here. Pointers are such a crucial concept, I don't mind having a beginners' section on them in the main docs, even though our main audience is supposed to understand them already. Reasonable people may disagree, however.