Why do we need reference variables?

let var = "aaa" ;

let var2 = &var ;
let var3 = &var ;
let var4 = &var ;
let var5 = &var ;
let var6 = &var ;
let var7 = &var ;
let var99 = &var ;

Why do we use var2, var3,var4,var5,var6,var8,var0, etc. , if we can simply use &var instead ?
What is the benefit of that ?

An interesting fact: var is a reference already…


Sometimes, the root expression isn’t as simple as var. For example, would you want to repeat &world[x_bin][y_bin][z_bin][index] all over the place, or would you prefer storing that in a variable for later use instead?

More fundamentally, a reference &T is a generic type very similar to MyStruct<T> and can be used in all the same ways: variables, function arguments, return values, vector elements, etc. Preventing a reference from being stored in a variable would be a needless restriction.


This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.