I have digested all your interventions, and that's thesum-up.
Point 1: lifetime vs scope
- "... lifetimes ensure that references are valid as long as we need them to be."
- "... lifetime, which is the scope for which that reference is valid."
Source: Validating References with Lifetimes
Thus, the term "lifetime" refers to references, while the term "scope" refers to variables.
From my point of view, the reference the refers to a (given) value and the referenced value are indissociable. They are like the two sides of the same coin.
- The lifetime of a reference or a (run-time) value.
- The scope of a variable.
I find this article very interesting. It talks about scope and lifetime in the context of other languages than Rust (for example: C). We definitely use the term "scope" for an "identifier" (that is, the variable) and the term "lifetime" for the "identified run-time object" (that is, the value).
Point 2: variable vs value
A variable temporarily owns a value <=> a value is temporarily owned by a variable.
Indeed, the ownership over a value may be moved from a variable to another variable.
// `a` is a pointer to a _heap_ allocated integer
let a = Box::new(5i32);
println!("a contains: {}", a);
// *Move* `a` into `b`
let b = a;
// The pointer address of `a` is copied (not the data) into `b`.
// Both are now pointers to the same heap allocated data, but `b` now owns it.
Source: https://doc.rust-lang.org/rust-by-example/scope/move.html
Point 3: references, variables and values
The &s1
syntax lets us create a reference that refers to the value of s1
.
source: References and Borrowing.
- References refer to values <=> values are referred to by references.
- Variables own values <=> values are owned by variables.
Please not that the value owned by a variable can be a reference.
Thus, I'd say:
The &s1
syntax lets us create a reference that refers to the value (temporarily) owned by (the variable) s1
.
Questions:
- Can we have a value that is not owned by a variable ? I'd say no. Otherwise, how could this value be accessed ?
- Is a value always referred to by a reference ? If a value is always owned by a variable, then it is always referred to by a reference.
The reference and the (referenced) value are like the 2 sides of the same coin. But the variable and the value (owned by the variable) are dissociable. Indeed, the (ownership over a) value can be moved from a variable to another. That's why the scope of a variable is not (always) the lifetime of the value (temporary) owned by the variable.
point 4: I'd like to distinguish between 3 things
- (the) "borrowed" (value): THE owned value being borrowed (the value is owned by a variable).
- (the) "borrow" (reference): THE (unique) reference that refers to a "borrowed" (value) <=>
&value
(value
being the variable that temporarily owns the value).
- (a) "borrower" (variable): A variable who (temporarily) owns THE "borrow" (the reference that refers to the temporarily owned value).
let borrower = &value
.
With all that precedes, we can reformulate:
Please note: if we accept the fact that the reference and the (referenced) value are like the 2 sides of the same coin (seen from 2 separate points of view), then they are the "same thing". And thus, as mentioned earlier in this post, we could use the same word. However, using 2 separate words allows us to implicitly specify the adopted "point of view". Specifying the adopted point of view may make things clearer.
// Using Rust terminology: the filefime of the value temporarily owned by the variable "i" outlives the scope of the (run-time) variable "i".
void p() {
static int i = 0;
print(i++);
}
What do you think if these formulations ?
Thanks !