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

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);
主站蜘蛛池模板: 庆阳市| 双牌县| 武清区| 昆山市| 梨树县| 内丘县| 平江县| 广丰县| 灌南县| 习水县| 敦化市| 阳高县| 寿宁县| 夏河县| 宁津县| 历史| 和平县| 呼和浩特市| 邓州市| 广灵县| 双城市| 屯门区| 葫芦岛市| 汤阴县| 南部县| 和政县| 尚志市| 淳安县| 兴国县| 托里县| 临颍县| 洪雅县| 五莲县| 枞阳县| 临澧县| 临安市| 娄烦县| 河北区| 如皋市| 清涧县| 商洛市|