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

  • 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);
主站蜘蛛池模板: 通许县| 佳木斯市| 濮阳市| 岳阳县| 高州市| 汝州市| 凉山| 清新县| 新安县| 视频| 当阳市| 儋州市| 承德市| 松溪县| 江安县| 香河县| 洛浦县| 和田市| SHOW| 兰考县| 武功县| 读书| 霸州市| 高邑县| 南通市| 巴林左旗| 富蕴县| 牡丹江市| 应城市| 得荣县| 平武县| 濮阳市| 徐州市| 神池县| 和硕县| 溆浦县| 枞阳县| 高邮市| 临潭县| 临高县| 庆安县|