-
Notifications
You must be signed in to change notification settings - Fork 106
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
Struct lifetimes could use clarification #210
Comments
To answer your specific questions:
The opposite, ref fields must outlive structs containing them.
A struct can contain references that are guaranteed to live for different lengths of time. Both lengths of time are longer than the struct's lifetime, but are not necessarily equal. For example: struct Foo<'a, 'b> {
x: &'a i32,
y: &'b i32
}
struct Bar<'a> {
x: &'a i32,
y: &'a i32
}
fn works() {
let a = 1;
let x = &a;
let z = {
let b = 2;
let y = &b;
let foo = Foo { x, y };
foo.x
};
println!("{z}");
}
fn does_not_work() {
let a = 1;
let x = &a;
let z = {
let b = 2;
let y = &b;
let foo = Bar { x, y };
foo.x
};
println!("{z}");
} The first function compiles while the second one does not. They are the same except for the struct being used. In the second function, the struct
The annotations are definitely also for the compiler, as shown in the example above. Your general point about explaining this more in the book is well-taken, I'll see how to work it in. |
Oops yes, I wrote that first one backwards. :-D And thanks for the example, that's helpful! Something like that would be really useful in illustrating that concept. I think the last thing that I'm shaky on is the distinction between the lifetime parameters for the struct reference vs. the struct type. In the book we have |
lifetimes
lifetime
main
branch to see if this has already been fixed, in this file:src/ch10-03-lifetime-syntax.md
URL to the section(s) of the book with this problem:
https://rust-book.cs.brown.edu/ch10-03-lifetime-syntax.html#lifetime-annotations-in-struct-definitions
Description of the problem:
While the discussion of lifetimes as they relate to functions is quite helpful, there's very little here about how structs are related to lifetimes. Don't struct instances always have to outlive their ref fields? What does it mean for a struct to have multiple lifetime annotations? What is being enforced differently if I have multiple fields with the same lifetime vs. different ones?
Suggested fix:
Broadly, it would help to have those questions answered! I'm still learning Rust, so I can't suggest authoritative answers. However, from IRC, I got the following notes that made sense to me:
new
method (for instance) relate to the struct's lifetimes.The text was updated successfully, but these errors were encountered: