Skip to content

New ownership guide#17138

Closed
steveklabnik wants to merge 5 commits into
rust-lang:masterfrom
steveklabnik:new_ownership_guide
Closed

New ownership guide#17138
steveklabnik wants to merge 5 commits into
rust-lang:masterfrom
steveklabnik:new_ownership_guide

Conversation

@steveklabnik

Copy link
Copy Markdown
Contributor

This is the start of the new "Ownership" guide, to replace the lifetimes guide.

I'm still working on it, but I wanted to share my progress, as this guide is very important, and I'd like as many eyes on it as possible. 😄

@steveklabnik

Copy link
Copy Markdown
Contributor Author

I also only deprecated the old one 😉 ❤️ @huonw

@Manishearth

Copy link
Copy Markdown
Member

Yay! I'll read through the whole thing when I get time. Couple of things:

  • Could this guide also explain the difference between moved and copied types?
  • It should probably discuss clone() too, and any other common errors and how to escape them

Comment thread src/doc/guide-ownership.md Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

typo, lifetime

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

sigh

@steveklabnik

Copy link
Copy Markdown
Contributor Author

@Manishearth move semantics aren't exactly the same thing as ownership. clone() is something that will end up being discussed, of course.

@Manishearth

Copy link
Copy Markdown
Member

Hm, agreed, but I found them closely related. While learning Rust at first I didn't realize the difference between move/copy values and thus confused myself a lot. It took me a while to consolidate &/&mut, ~ (now box), clone, ref, and move/copy semantics and understand the memory model properly enough to use it without fighting the compiler. While they're theoretically something different, practically they turn up in similar places. But I guess discussing it elsewhere is okay, as long as they are discussed :)

Comment thread src/doc/guide-ownership.md Outdated

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

SomeStrut -> SomeStruct?

@huonw

huonw commented Sep 11, 2014

Copy link
Copy Markdown
Contributor

move semantics aren't exactly the same thing as ownership

Ownership implies move semantics, and values moving are one of the major ways people hit ownership... they're intimately linked.

(e.g. taking a x: &T to y: T via y = *x requires taking the data out of x by value and placing it into y, which requires transferring ownership, cf. my answer here.)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

That sounds like what lives in the heap is a Box<int>. But that's not true. In your case x living on the stack would be a Box<int>, a handle you can use to reach the heap-allocated int.

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.