Created
January 26, 2020 19:29
-
-
Save alcuadrado/6051fa2ad82024aad923bfba99f45ffd to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#![allow(dead_code)] | |
#![allow(unused_variables)] | |
#[derive(Debug)] | |
struct Point { | |
x: f32, | |
y: f32, | |
} | |
#[derive(Debug)] | |
struct Rectangle { | |
top_left: Point, | |
bottom_right: Point, | |
} | |
fn main() { | |
let point: Point = Point { x: 10.3, y: 0.4 }; | |
let Point { x, y } = point; | |
println!("point coordinates: ({}, {})", point.x, point.y); | |
let bottom_right = Point { x: 5.2, ..point }; | |
println!("second point: ({}, {})", bottom_right.x, bottom_right.y); | |
let Point { x: top_edge, y: left_edge } = point; | |
let rectangle = Rectangle { | |
top_left: Point { x: left_edge, y: top_edge }, | |
bottom_right: bottom_right, | |
}; | |
println!("Rectangle {:?} with area {:?}", rectangle, rect_area(rectangle)); | |
} | |
fn rect_area(rect: Rectangle) -> f32 { | |
let Rectangle { | |
top_left: Point { | |
x, | |
y: ytl | |
}, | |
bottom_right: Point { | |
x: xbr, | |
y: ybr | |
} | |
} = rect; | |
return (xbr - x) * (ytl - ybr) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment