34 lines
590 B
Rust
34 lines
590 B
Rust
// $ rustc ownersystem_4_mutable.rs
|
|
// $ ./ownersystem_4_mutable
|
|
struct Bob {
|
|
n: Vec<i32>
|
|
}
|
|
|
|
impl Bob {
|
|
fn new() -> Bob {
|
|
Bob { n: Vec::new() }
|
|
}
|
|
}
|
|
|
|
fn func_1() {
|
|
let mut n = Bob::new();
|
|
let n1 = &mut n;
|
|
// doesn't work because there is already a mutable borrowing
|
|
//let n2 = &n;
|
|
func_2(n1);
|
|
//func_3(n2);
|
|
//works because both func_2 and func_3 only got read only references
|
|
println!("{}", n.n[0]);
|
|
}
|
|
|
|
fn func_2(b: &mut Bob) {
|
|
println!("{}", b.n[0]);
|
|
}
|
|
|
|
fn func_3(b: &Bob) {
|
|
println!("{}", b.n[0]);
|
|
}
|
|
|
|
fn main() {
|
|
func_1();
|
|
} |