The reference type

A reference is written in one of three ways: &, ref, or ref mut:

let mut var = 4; 
let ref_to_var = &var; 
let ref second_ref = var; 
let ref mut third_ref = var; 

The references are all equivalent here. Note, however, that the preceding code doesn't work as it is due to mutable reference rules. Rust allows several immutable reference to a thing, but if a mutable reference is taken, no other references may exist at the time. Therefore, the last line would not work, since there are already two active references to var.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset