天天看點

012 Rust死靈書之分解借用

介紹

本系列錄制的視訊主要放在B站上​​Rust死靈書學習視訊​​

Rust相關的源碼資料在:​​https://github.com/anonymousGiga​​

筆記内容

//以下代碼可以運作
struct Foo {
    a: i32,
    b: i32,
    c: i32,
}

let mut x = Foo {a: 0, b: 0, c: 0};
let a = &mut x.a;
let b = &mut x.b;
let c = &x.c;
*b += 1;
let c2 = &x.c;
*a += 10;
println!("{} {} {} {}", a, b, c, c2);      
let mut x = [1, 2, 3];
let a = &mut x[0];
let b = &mut x[1];
println!("{} {}", a, b);