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

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);
主站蜘蛛池模板: 商洛市| 霞浦县| 平陆县| 曲阜市| 新乡市| 樟树市| 永宁县| 靖远县| 南华县| 莒南县| 太仆寺旗| 玛多县| 健康| 连平县| 武平县| 佛教| 金门县| 灵川县| 依兰县| 淮阳县| 额济纳旗| 交城县| 定襄县| 锡林浩特市| 沂源县| 余庆县| 梁平县| 新丰县| 遂平县| 宁津县| 南召县| 河曲县| 宁强县| 谢通门县| 梁河县| 禹州市| 黄大仙区| 互助| 德钦县| 长岭县| 麦盖提县|