I have been learning Rust occasionally for a while and I have a lot of questions about explicit lifetimes. Some of the answers might be found in the documents but I still want to make sure I really get it.
-
Given that only references, as opposed to objects, can be borrowed in the situations of arguments passing, does that mean explicit lifetime annotations are only associated with parameters of reference type?
-
Following the above, which means if a function does not have any reference type in its function signature (parameter and return value) then it doesn't bother with explicit lifetime annotations at all. Right?
-
A lifetime variable is meaningful only if it creates a relation between two or more values/references. Which means a lifetime variable
'a
must associate something with unknown lifetime to something else with a known lifetime in order to propagate the lifetime information. Something likefn<'a> foo(x : &'a str) int
orfn<'a> bar(x : &str) -> &'a str
doesn't convey any useful lifetimes information, much like the 'defined by unused variables'. Am I correct? -
In the case of struct declarations, does the lifetime annotations mean the lifetime relationships between the containing struct object and its fields (of reference type) ?
-
If lifetime annotations create relation between two references/objects, can I say something like the following:
a. Define a binary operator~
b.A ~ B
means A and B belong to the same lifetime (or in other words, has the same lifetime value).
c. Thenfn<'a> foo(x : &'a str) -> &'a str
can be interpreted as:
Given that x has an initialized lifetime L denoted by the variable'a
, the returned str reference has an uninitialized (unknown) lifetimeb
,
We declarea ~
b, so the compiler can assign L to both the variable 'a and 'b (or IOW to propagate L from 'a to 'b)
This is what all I have imagined about lifetimes annotations.
Do I get something wrong?