error[E0495]: cannot infer an appropriate lifetime for lifetime parameter `'a` due to conflicting requirements
--> src/lib.rs:11:9
|
11 | WithRef{r: src.r}
| ^^^^^^^
|
note: first, the lifetime cannot outlive the anonymous lifetime defined here...
But for me it doesn't make sense because the lifetime of WithMutRef is irrelevant here. All we need is to make sure that those two structs references a data with the same lifetime. What am I missing?
The problem is that a mutable reference must have exclusive access. Any time you create a "sub-reference" from the mutable reference, the mutable reference must be disabled for the duration of that sub-reference, since using the mutable reference while the sub-reference exists contradicts the exclusivity of the mutable reference.
The way that this manifests in lifetimes is that &'short &'long mut T flattens to &'short T rather than &'long T, preventing the flattened reference from being used for longer than the &'short &'long mut T could be used. This is different from what happens when the inner reference is immutable, as there's no such exclusivity requirement for immutable references.
I really don't understand how lifetimes are combined in this fashion. I used to think that lifetime is an immanent property of data being referenced, basically it's scope.
A lifetime is the duration in which a variable is borrowed, not the duration in which the variable exists.
(Technically speaking, the lifetime is also allowed to be shorter than the duration in which the variable is borrowed. For example, given an &'long T, you can convert it to an &'short T even though this doesn't change how long the borrow lasts for.)
Another way to see lifetimes is that if you have a &'a T, then that's a pointer along with a promise that its safe to use it anywhere inside the region 'a.