-
Notifications
You must be signed in to change notification settings - Fork 13k
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
New ownership guide #17138
New ownership guide #17138
Conversation
I also only deprecated the old one 😉 ❤️ @huonw |
Yay! I'll read through the whole thing when I get time. Couple of things:
|
These two are related. Changing `struct SomeStruct` to `struct | ||
SomeStruct<'struct>` adds a **lifetime parameter**. This parameter gives a | ||
name to the lifetime of a reference to this struct. In this case, we've given | ||
it the name 'struct,' since that's what it's the lieftime of. You can use any |
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.
typo, lifetime
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.
sigh
@Manishearth move semantics aren't exactly the same thing as ownership. |
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 |
structure itself, but the structure _also_ contains a pointer to a `str`. We | ||
have no guarantee that the reference inside of `a_string` is valid for the | ||
entire time that the reference to the `SomeStruct` itself. If it's not, then we | ||
could have a valid reference to a `SomeStrut` which contained an invalid |
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.
SomeStrut -> SomeStruct?
Ownership implies move semantics, and values moving are one of the major ways people hit ownership... they're intimately linked. (e.g. taking a |
24f0d34
to
941ca29
Compare
} | ||
``` | ||
|
||
The `box` keyword allocates memory on the heap, and places a `Box<int>` there. |
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.
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
.
} | ||
|
||
fn main() { | ||
let x = box SomeStruct { a_string: "A string" }; |
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 x is a pointer.
If you could also include an example and short explanation of situations in which references to traits require lifetime bounds that'd be awesomely helpful! I still don't quite have a clear grasp on why they're required as a bound when you already provide a lifetime with the reference? |
I feel like calling this just an ‘Ownership Guide’ is a bit confusing. Sure, ownership can be a source of many problems for newcomers, but I suspect that another big reason people will come here is to learn about lifetimes after getting a confusing error about them. Calling it just an ‘Ownership Guide’ would make it much harder to find. Perhaps this could be the ‘Ownership and Lifetimes Guide’, if only for the sake of discoverability? I suppose that if this is linked to from the tutorial it wouldn’t be too bad, but chances are at least some people will skip that link until they bump into a lifetime-/ownership-related error. |
You sneak a "box" in there when you get the code compiling. You explicitly point out your addition of the lifetime parameter, but you sneak the "box" in without discussing it. Would it not compile without box? It appears like it would, but I haven't tried it. I just know as a learner, if I didn't know what box meant, I'd no longer feel like I was able to follow your discussion. |
I think both code examples originally had |
Of course, it would be very limiting to only have a single possible pointer to | ||
a resource. If you need additional pointers, Rust offers a second concept: | ||
borrowing. Another reference may borrow the contents of a pointer which has | ||
ownership. Here's an example: |
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 don't know Rust, but this bit seems confusing. You seem to imply that x
in the example below is a pointer and it doesn't look like it in the code below.
I'm not sure how to rephrase it best if that's not the case...
There's enough random problems with this, and it's already out of date, so I'm just going to give it a close and make a new draft instead. |
…-new, r=Veykril feature: Make generate function assist generate a function as a constructor if the generated function has the name "new" and is an asscociated function. close rust-lang#17050 This PR makes `generate function assist` generate a function as a constructor if the generated function has the name "new" and is an asscociated function. If the asscociate type is a record struct, it generates the constructor like this. ```rust impl Foo { fn new() -> Self { Self { field_1: todo!(), field_2: todo!() } } } ``` If the asscociate type is a tuple struct, it generates the constructor like this. ```rust impl Foo { fn new() -> Self { Self(todo!(), todo!()) } } ``` If the asscociate type is a unit struct, it generates the constructor like this. ```rust impl Foo { fn new() -> Self { Self } } ``` If the asscociate type is another adt, it generates the constructor like this. ```rust impl Foo { fn new() -> Self { todo!() } } ```
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. 😄