官术网_书友最值得收藏!

  • Rust Programming By Example
  • Guillaume Gomez Antoni Boucher
  • 87字
  • 2021-07-02 19:12:57

Clone types

An alternative to using references is to clone values. By cloning a value, we don't move it. To be able to clone a point, we can add derive to it:

#[derive(Clone, Debug)]
struct Point {
    x: i32,
    y: i32,
}

We can now call the clone() method to avoid moving our p1 point:

fn print_point(point: Point) {
    println!("x: {}, y: {}", point.x, point.y);
}

let p1 = Point { x: 1, y: 2 };
let p2 = p1.clone();
print_point(p1.clone());
println!("{}", p1.x);
主站蜘蛛池模板: 巴马| 柳州市| 道孚县| 万年县| 惠州市| 汉阴县| 盈江县| 噶尔县| 弥勒县| 库伦旗| 蕉岭县| 宣威市| 长泰县| 乌拉特后旗| 丰顺县| 杨浦区| 长宁县| 开鲁县| 襄樊市| 尼勒克县| 鹿邑县| 唐海县| 樟树市| 利津县| 潜山县| 当雄县| 库伦旗| 锡林浩特市| 元氏县| 洞口县| 苗栗市| 肃北| 阿拉善左旗| 钟山县| 富源县| 防城港市| 渭南市| 雷波县| 治县。| 安达市| 巴东县|