Читаем Rust by Example полностью

let mutable_borrow = &mut point;

// Change data via mutable reference

mutable_borrow.x = 5;

mutable_borrow.y = 2;

mutable_borrow.z = 1;

// Error! Can't borrow `point` as immutable because it's currently

// borrowed as mutable.

// let y = &point.y

// TODO ^ Try uncommenting this line

// Error! Can't print because `println!` takes an immutable reference.

// println!("Point Z coordinate is {}", point.z);

// TODO ^ Try uncommenting this line

// Ok! Mutable references can be passed as immutable to `println!`

println!("Point has coordinates: ({}, {}, {})",

mutable_borrow.x, mutable_borrow.y, mutable_borrow.z);

// The mutable reference is no longer used for the rest of the code so it

// is possible to reborrow

let new_borrowed_point = &point

println!("Point now has coordinates: ({}, {}, {})",

new_borrowed_point.x, new_borrowed_point.y, new_borrowed_point.z);

}

הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

<p id="the_ref_pattern"><strong><a l:href="#the_ref_pattern">The ref pattern</a></strong></p>

When doing pattern matching or destructuring via the let binding, the ref keyword can be used to take references to the fields of a struct/tuple. The example below shows a few instances where this can be useful:

#[derive(Clone, Copy)]

struct Point { x: i32, y: i32 }

fn main() {

let c = 'Q';

// A `ref` borrow on the left side of an assignment is equivalent to

// an `&` borrow on the right side.

let ref ref_c1 = c;

let ref_c2 = &c

println!("ref_c1 equals ref_c2: {}", *ref_c1 == *ref_c2);

let point = Point { x: 0, y: 0 };

// `ref` is also valid when destructuring a struct.

let _copy_of_x = {

// `ref_to_x` is a reference to the `x` field of `point`.

let Point { x: ref ref_to_x, y: _ } = point;

// Return a copy of the `x` field of `point`.

*ref_to_x

};

// A mutable copy of `point`

let mut mutable_point = point;

{

// `ref` can be paired with `mut` to take mutable references.

let Point { x: _, y: ref mut mut_ref_to_y } = mutable_point;

// Mutate the `y` field of `mutable_point` via a mutable reference.

*mut_ref_to_y = 1;

}

println!("point is ({}, {})", point.x, point.y);

println!("mutable_point is ({}, {})", mutable_point.x, mutable_point.y);

// A mutable tuple that includes a pointer

let mut mutable_tuple = (Box::new(5u32), 3u32);

{

Перейти на страницу:

Похожие книги