It is from Rust by example's activities. The complier said "error[E0382]: use of moved value: point
". I have tried to pass by reference and deref but got other error.
It seems "top_left: point" has moved ownership. I don't know how to make it works.
// An attribute to hide warnings for unused code.
#![allow(dead_code)]
// A struct with two fields
struct Point {
x: f32,
y: f32,
}
// Structs can be reused as fields of another struct
struct Rectangle {
// A rectangle can be specified by where the top left and bottom right
// corners are in space.
top_left: Point,
bottom_right: Point,
}
fn main() {
// Instantiate a `Point`
let point: Point = Point { x: 10.3, y: 0.4 };
// Access the fields of the point
println!("point coordinates: ({}, {})", point.x, point.y);
fn square(point: Point, w: f32) -> Rectangle {
Rectangle {
top_left: point,
bottom_right: Point {
x: point.x + w,
y: point.y - w,
},
}
}
let rect = square(point, 0.2);
println!(
"({},{}) ({},{})",
rect.top_left.x, rect.top_left.y, rect.bottom_right.x, rect.bottom_right.y
);
}