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

Guide Redux: Pointers #15789

Closed
wants to merge 1 commit into from

Conversation

steveklabnik
Copy link
Member

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.

^
```

We can **dereference** the pointer by using the `*` operator. This will work:
Copy link

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 😄

Copy link
Contributor

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:

Copy link

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

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed. Updated.

@cburgdorf
Copy link
Contributor

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

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?

Copy link
Member Author

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.

@dreoliv
Copy link

dreoliv commented Jul 18, 2014

👍 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
```
Copy link
Member

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

Copy link
Member Author

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.

Copy link
Member

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.

Copy link
Member Author

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?

@bachm
Copy link

bachm commented Jul 19, 2014

It would generally be useful to have a cheat sheet showing pointer types and their typical usage with cell types. Something like:

Name Representation Purpose
Reference &T Allows one or more entities to read T.
Mutable reference &mut T Allows a single entity to read from and write to T.
Reference and Cell &Cell<T> Allows one or more entities to read from and write to T, provided T implements the Copy trait.
Owned pointer (Unique pointer?) Box<T> Allocates T on the heap, with unique ownership, allowing read and write access to T.
Rc pointer Rc<T> Allocates T on the heap, with shared ownership, allowing read access to T.
Rc pointer and RefCell Rc<RefCell<T>> Same as above, plus one-at-a-time write access to T.
Arc pointer Arc<T> Allocates T on the heap, with shared ownership across thread boundaries, allowing read access to T.
Arc pointer and Mutex Arc<Mutex<T>> Same as above, plus one-at-a-time write access to T.
Raw pointer *const T Unrestrained and potentially unsafe read access to T.
Raw mutable pointer *mut T Same as above, plus unrestrained and potentially unsafe write access to T.

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 Arc<T> as Arc pointer or atomically reference counted pointer?

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.

@steveklabnik
Copy link
Member Author

@bachm, I really like this idea.

@steveklabnik
Copy link
Member Author

I added the cheat sheet, and modified it a bit

@steveklabnik
Copy link
Member Author

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.

@steveklabnik
Copy link
Member Author

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">
Copy link
Member

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?

Copy link
Member Author

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.

Copy link
Member

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.

Copy link
Member Author

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.

Copy link
Member

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

@bluss
Copy link
Member

bluss commented Jul 21, 2014

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.

@steveklabnik
Copy link
Member Author

The line gets very blurry. I consider anything that implements Deref and DerefMut to be a 'smart pointer.'

@bluss
Copy link
Member

bluss commented Jul 21, 2014

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 -- Ref and RefMut. But so has Mutex<T>, it has MutexGuard which is a smart pointer and a RAII guard at the same time.

@steveklabnik
Copy link
Member Author

Okay, addressed everything else so far.

bors added a commit that referenced this pull request Jul 26, 2014
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.
@bors bors closed this Jul 26, 2014
@steveklabnik steveklabnik deleted the guide_pointers branch July 30, 2014 13:48
bors added a commit to rust-lang-ci/rust that referenced this pull request Nov 13, 2023
Store binding mode for each instance of a binding independently

fix rust-lang#15787
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.

None yet