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
'amust associate something with unknown lifetime to something else with a known lifetime in order to propagate the lifetime information. Something like
fn<'a> foo(x : &'a str) intor
fn<'a> bar(x : &str) -> &'a strdoesn’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
A ~ Bmeans A and B belong to the same lifetime (or in other words, has the same lifetime value).
fn<'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?