From the Rust by exemples web pages, I have this code:
struct Point {
x: f32,
y: f32,
}
struct Rectangle {
top_left: Point,
bottom_right: Point,
}
I added a function to make a square out of a point (reference) and a side length:
fn square(point: &Point, side: f32) -> Rectangle {
let Point { x, y } = point;
return Rectangle{top_left: Point{x: x,
y: y
},
bottom_right: Point{x: x + side,
y: y + side}
};
}
As a shortcut I recover the x and y fields as variables.
The compiler outputs:
error[E0308]: mismatched types
--> ch3_1structs_b.rs:39:41
|
39 | return Rectangle{top_left: Point{x: x,
| ^ expected `f32`, found `&f32`
|
help: consider dereferencing the borrow
|
39 | return Rectangle{top_left: Point{x: *x,
| +
(same for y)
So I fixed it with:
fn square(point: &Point, side: f32) -> Rectangle {
let Point { x, y } = point;
return Rectangle{top_left: Point{x: *x,
y: *y
},
bottom_right: Point{x: x + side,
y: y + side}
};
}
And the compiler is now happy.
What I don't understand is why the dereferences were not needed for bottom_right as well:
bottom_right: Point{x: *x + side,
y: *y + side}
};
???