[][src]Function tao_of_rust::ch05::borrow::borrow

pub fn borrow()

借用与引用

Base usage: 为函数传递数组,使用可变绑定作为参数 因为传入的是不可变,而经过函数参数的模式匹配,成为了可变参数

fn foo(mut v: [i32; 3]) -> [i32; 3] {
    v[0] = 3;
    assert_eq!([3,2,3], v);
    v
}
fn main() {
    let v = [1,2,3];
    foo(v);
    assert_eq!([1,2,3], v);
}Run

Base usage: 以可变引用作为参数

fn foo(v: &mut [i32; 3]) {
    v[0] = 3;
}
fn main() {
    let mut v = [1,2,3];
    foo(&mut v);
    assert_eq!([3,2,3], v);
}Run