Hello Quinedot,
Thank you very much for your responses ! It helped me a lot.
You point out an interesting point : usually (as far as I've noticed), we talk about the lexical scope (of a variable). When I talk about "scope
" with students, they immediately think "lexical."
Please note that this may be a French particularity.
Thus, in order to avoid misunderstanding, it is best to use this term to refer to a lexical scope only (because we can think of other types of scopes, of course), and to always use it in conjunction with the adjective "lexical."
But
lifetimes
haven't been restricted tolexical scopes
since 2018.
Yes, we agree. The term "lexical scope
" applies to a variable, while the "lifetime
" applies to a value. And it is true that a value may very well outlive a variable that temporarily identifies it. That's typically the case, in C programming, when (from within a function) you return a pointer to a memory allocation buffer. The value (the content of the buffer) still exists outside of the function, while the (local) variable within the function that referred to the buffer vanished.
The liveness of a borrower cannot outlive the borrow lifetime. I.e., the borrower must not be used (or usable) outside of the borrow lifetime. But that liveness region might not correspond to a lexical / drop scope.
Given the definition "We say that a variable is live (alive ?) if the current value that it holds may be used later" : that seems perfectly logic.
Is the concept of "liveness
" used within a C compiler ?
I mean : in C programming, you can (by mistake) use a variable, while the value it "held" (I used the past) is not usable anymore. The C compiler does not check whether or not it is fine to use the value held by a variable. How could the C compiler could find out that a value is not usable anymore, anyway ? It seems impossible.
The scope of a
&T
or&mut T
variable never really matters. The scope of a struct containing such a borrower can matter, if it has a destructor -- because that destructor will run at the end of the scope.
That's clear.
The scope of borrowed -- the
T
variable you're creating a&T
to -- is an upper limit on the borrow lifetime.
That's clear.
Rust does have "raw pointers" in addition to references.
I haven't seen "raw pointers" yet. I imagine that "raw pointers" are necessary, in other things, to interface with C binaries. But one step at a time. I'll see that later.
Thank again !