Each “where” is called a plane, which is a tree. The stack plane contains all your function calls memory, a structure allocated on the stack has a child plane for each field. The compiler tags which plane something is from at compile time and tracks when it dies.
Things do get complicated in scenarios like:
fn max(a: &usize, b: &usize) -> &usize {
if *a > *b { a } else { b }
}In such a scenario the compiler tags the result with both of the input planes. However, you cannot define add a postcondition ensures clause to make it more precise
[–] 0c3ca83 3 days ago
[–] jordan_bonecut 3 days ago
[–] 0c3ca83 3 days ago
[–] jordan_bonecut 3 days ago
[–] 0c3ca83 3 days ago
[–] jordan_bonecut 3 days ago
let x = vec![1, 2, 3]; let a = &mut x[0]; let b = &mut x[1]; swap(a, b);
Because Rust doesn't really know the difference between the whole Vec an element within the vec. Therefore it treats any mutation as one that could potentially invalidate anything with the same lifetime and doesn't compile. So I believe the plane model to be strictly better because it allows for this and still prevents the real scary thing:
let x = vec![1, 2]; let a = &mut x[0]; x.push(3); a = a + 1;
[–] tancop 3 days ago