-
Notifications
You must be signed in to change notification settings - Fork 58
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
Claim of lifetime sizes in 00_welcome #18
Comments
Hey! So there are actually a couple things going on here, apologies if this is unstructured; and also apologies if I over-explain things (if nothing else, hopefully I can point other people here with similar issues). First: the example you gave actually isn't quite doing what you think it is. This is because A code example which gets around this issue is: #[derive(Debug)]
struct NotCopy {
i: i32
}
fn main() {
let mut my_reference: Option<&NotCopy> = None;
// Starting a scope.
{
// my_variable created // \ \
let my_variable: NotCopy = NotCopy { i : 7}; // | |
my_reference = Some(&my_variable); // | |- my_variable exists here. ('variable)
// At the end of the scope, `my_variable` is dropped // | |
drop(my_variable); // | |
// my variable destroyed // | /
// inner scope does not stop here }
// |
if let Some(reference) = my_reference { // |
println!("{reference:?}"); // |
} // /
} // inner scope now stops here
} But what you'll see in this example is that we do run into an issue:
The error here is with the I do think that the wording here could be improved, and I'm gonna have a think about how to do that (suggestions welcome) but before I do, I'd like to know if this explanation helped, or if there's something you're still confused about. That'll help me figure out what the best wording actually is! Thanks for opening an issue! |
Hey @tfpk , thanks for responding promptly! I think I follow what you're getting at with So reading 00_example, I chucked this into the playground and it works. ie, putting println! inside the inner scope, but after the drop prints 7 (https://play.rust-lang.org/?version=beta&mode=debug&edition=2021&gist=62de6293aff89f91d15e7d57b9446bce). So now I'm confused about several things.
I think there's two things here, a simpler explaination of lifetime and an accidental complexity introduced by this example. And we're now talking about both! |
Sorry to pile on the confusion, let's try get it straightened out...
To help with 1 and 2, the reading the documentation of the
#[derive(Debug)]
struct NotCopy {
i: i32
}
fn main() {
let mut my_reference: Option<&NotCopy> = None; // lifetime of my_reference starts
let my_value: NotCopy = NotCopy { i: 8 }; // lifetime of my_value starts
my_reference = Some(&my_value);
if let Some(val) = my_reference {
println!("{val:?}");
}
} Which seems to disprove my comment made in section 0. The trick here is that there are two seperate lifetimes which are being conflated here: the lifetime of It is clear that the variables Okay I've had this checked; pretty sure it's all reasonable. |
I've tried to improve the wording in #19, but it's a first draft which I'll have to revise again, I suspect. |
Towards the end of the first chapter, once the
'variable
and'reference
lifetimes has been established, there is the following statement:The last part of this statement is confusing to me because it does not seem to be true for all cases.
Let's say in the code example, that the inner scope of
'variable
is expanded to include theif let
statement:It is not true that
'variable
is now larger than'reference
, because'reference
is declared earlier still.So this is confusing to me. It would make more sense if it read:
The text was updated successfully, but these errors were encountered: