fn main() {
let s = String::from("hello");
let len = calculate_length(&s); // borrow s immutably
println!("The length of '{}' is {}.", s, len); // s is still valid
}
fn calculate_length(s: &String) -> usize {
s.len()
}
- Immutable references (
&T
) allow read-only access. - Mutable references (
&mut T
) allow read-write access but are exclusive. - You cannot have mutable and immutable references to the same data simultaneously.