-
Notifications
You must be signed in to change notification settings - Fork 13.2k
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
Ownership guide #18613
Ownership guide #18613
Conversation
@steveklabnik to what extent do you want this reviewed right now? |
However, this system does have a certain cost: learning curve. Many new users | ||
to Rust experience something we like to call "fighting with the borrow | ||
checker," where the Rust compiler refuses to compile a program that the author | ||
thinks is valid. You probably will experience similar things at first. There is |
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.
Is it worth putting something like "(experience has shown that the compiler is often right)" here, or is that too twerpish?
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 was really tempted to write it, but had the same wonder.
Sent from my iPhone
On Nov 4, 2014, at 13:56, Huon Wilson notifications@github.com wrote:
In src/doc/guide-ownership.md:
+# Meta
+
+Before we get to the details, two important notes about the ownership system.
+
+Rust has a focus on safety and speed. It accomplishes these goals through "zero
+cost abstractions," which means that in Rust, abstractions cost as little as
+possible in order to make them work. The ownership system is a prime example of
+a zero-cost abstraction. All of the analysis we'll talk about in this guide is
+done at compile time. You do not pay any run-time cost for all of these
+features.
+
+However, this system does have a certain cost: learning curve. Many new users
+to Rust experience something we like to call "fighting with the borrow
+checker," where the Rust compiler refuses to compile a program that the author
+thinks is valid. You probably will experience similar things at first. There is
Is it worth putting something like "(experience has shown that the compiler is often right)" here, or is that too twerpish?�
Reply to this email directly or view it on GitHub.
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'd say 👎
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.
Another way you could phrase it (that being the sentiment of "the compiler is often right") that's less dismissive of the reader: "this often happens because the programmer's mental model of how ownership should work doesn't match the actual rules that Rust implements." This could then lead into talking about how, as you gain experience with Rust, you start to internalise the rules.
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 like it. incoming!
Well this is the first draft of the first fourth or so, so anything that comes to mind, really. Structure, depth, fact-checking, whatever :)= |
|
||
There's one other important detail with regards to allocating memory. Whenever | ||
we request some amount of memory, what we are given back is a handle to that | ||
memory. This handle is how we interact with the allocated memory. As long as we |
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.
In the vein of concreteness, is it worth clarifying that it is a pointer? (e.g. "what we are given back is a handle to that memory (a pointer). This ...")
I guess some people might be confused calling it a handle without acknowledging the conventional term (i.e. be asking "is this handle somehow different to a pointer?? Is my life a lie?" etc. etc.)
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, maybe. I can totally see this. hmm.
@simias I feel like the word "ideal" is supposed to mean that the code shown above can be written in a better, more robust way, despite the fact that it "works" as is. |
On Wed, Nov 05, 2014 at 07:33:37AM -0800, Matej Ľach wrote:
Aaaah, I misunderstood what was meant here. I thought the remark was Lionel Flandrin |
Yes, that is exactly correct. |
|
||
# Ownership | ||
|
||
At its core, ownership is about 'resources.' For the purposes of the vast |
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.
You introduce 'resources' here but don't define them. Maybe something like:
"At its core, ownership is about 'resources', which are anything that must be acquired before using, and released or closed when no longer needed."
} | ||
``` | ||
|
||
The `box` keyword creates a `Box<int>`, in this case. Boxes are allocated on |
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 this section may be clearer as:
The
box
keyword creates aBox
(specificallyBox<int>
in this case) by allocating a small segment of memory on the heap with enough space to fit anint
. But where does the code say the box is deallocated?
Or, the last sentence could be But when is the box deallocated?
.
(This is continuing @mdinger's point, but is a new thread so it's not lost in other old discussion.)
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.
+1
This is clearer.
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.
Ahhh, so this ties into the 'lifetime' vs 'scope' thing, I think. I prefer "where does it say" to subtly reinfore that this isn't a time-based thing, it's a static thing. But yeah, I sitll like the wording.
@thomasahle yes, it should be an &, thank you. |
2bdc9ae
to
7a5f8eb
Compare
7a5f8eb
to
a304adc
Compare
This replaces the previous "Lifetimes guide," since we are discussing things from an owernship perspective now.
a304adc
to
7213704
Compare
This is a work in progress, but this should get *extensive* review, so I'm putting it up early and often. This is the start of a draft of the new 'ownership guide,' which explains ownership, borrowing, etc. I'm feeling better about this framing than last time's, but we'll see.
This is a work in progress, but this should get extensive review, so I'm putting it up early and often.
This is the start of a draft of the new 'ownership guide,' which explains ownership, borrowing, etc. I'm feeling better about this framing than last time's, but we'll see.