Martin Odersky described in "Observers for linear types" ESOP'92
Linear types provide the framework for a safe embedding of mutable state in functional languages by enforcing the principle that variables of linear type must bc used exactly once. A potential disadvantage of this approach is that it places read accesses to such variables under the same restriction as write accesses [...]
Through references and borrowing, we can grant ownership for duration of a function call.
&mut T - temporary ownership&T (~ "observers")fn main() {
let s1 = String::from("hello");
let len = calculate_length(&s1);
println!("The length of '{}' is {}.", s1, len);
}
fn calculate_length(s: &String) -> usize {
s.len()
}
Borrowing is best understood as flow-sensitive type discipline.
let mut s = String::from("hello");
let r1 = &s; // no problem
let r2 = &mut s; // error "cannot borrow `s` as mutable because it is also borrowed as immutable"
Duration of temporary access: tracked through lifetime parameters.