You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hello, I'm currently going through "Rust by Example" and I'm very grateful for it, thank you. I was doing the activity which says to create a rect_area function to calculate the area of a Rectangle. When I completed this I noticed that I was getting a negative result when using my function on the already-created Rectangle in the default code. I found out that this is because the _rectangle.top_left.x coordinate is actually further right than the _rectangle.bottom_right.x coordinate, which is wrong-- bottom_right should have the right-most x value.
// Instantiate a `Point`let point:Point = Point{x:10.3,y:0.4};let another_point:Point = Point{x:5.2,y:0.2};// Make a new point by using struct update syntax to use the fields of our// other onelet bottom_right = Point{x:5.2, ..another_point };// Destructure the point using a `let` bindingletPoint{x: left_edge,y: top_edge } = point;let _rectangle = Rectangle{// struct instantiation is an expression tootop_left:Point{x: left_edge,y: top_edge },bottom_right: bottom_right,};
I've removed irrelevant code, but notice how the bottom_right of _rectangle is actually on the bottom left, and the top_left is actually on the top right!
Finding the area of a rectangle with these coordinates (if one goes by the names of the fields) would be (x2-x1)* (y2-y1) would yield a negative result! This can all be fixed by swapping the initial x values given on lines 50 and 51, and then also correcting the x value given on line 58.
Hello, I'm currently going through "Rust by Example" and I'm very grateful for it, thank you. I was doing the activity which says to create a
rect_area
function to calculate the area of a Rectangle. When I completed this I noticed that I was getting a negative result when using my function on the already-created Rectangle in the default code. I found out that this is because the_rectangle.top_left.x
coordinate is actually further right than the_rectangle.bottom_right.x
coordinate, which is wrong-- bottom_right should have the right-most x value.I've removed irrelevant code, but notice how the
bottom_right
of _rectangle is actually on the bottom left, and thetop_left
is actually on the top right!Finding the area of a rectangle with these coordinates (if one goes by the names of the fields) would be
(x2-x1)* (y2-y1)
would yield a negative result! This can all be fixed by swapping the initial x values given on lines 50 and 51, and then also correcting the x value given on line 58.It should be like this:
Thank you for your attention to this matter.
The text was updated successfully, but these errors were encountered: