
上QQ阅读APP看书,第一时间看更新
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);