26 lines
402 B
Rust
26 lines
402 B
Rust
// $ rustc ownersystem_2_borrowing.rs
|
|
// $ ./ownersystem_2_borrowing
|
|
struct Bob {
|
|
n: Vec<i32>
|
|
}
|
|
|
|
impl Bob {
|
|
fn new() -> Bob {
|
|
Bob { n: Vec::new() }
|
|
}
|
|
}
|
|
|
|
fn func_1() {
|
|
let n = Bob::new();
|
|
func_2(&n);
|
|
//is now in scope as func_2 just got a read only reference.
|
|
println!("{}", n.n[0]);
|
|
}
|
|
|
|
fn func_2(b: &Bob) {
|
|
println!("{}", b.n[0]);
|
|
}
|
|
|
|
fn main() {
|
|
func_1();
|
|
} |