You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
65 lines
1.3 KiB
65 lines
1.3 KiB
struct Nil;
|
|
|
|
struct Pair(i32, f32);
|
|
|
|
#[derive(Debug)]
|
|
struct Point {
|
|
x: f32,
|
|
y: f32,
|
|
}
|
|
|
|
#[derive(Debug)]
|
|
struct Rectangle {
|
|
p1: Point,
|
|
p2: Point,
|
|
}
|
|
|
|
fn rect_area(rect: Rectangle) -> f32 {
|
|
let Rectangle {
|
|
p1: Point { x: x1, y: y1 },
|
|
p2: Point { x: x2, y: y2 }
|
|
} = rect;
|
|
((x1 - x2) * (y1 - y2)).abs()
|
|
}
|
|
|
|
fn square(point: Point, dim: f32) -> Rectangle {
|
|
Rectangle {
|
|
p1: Point { x: point.x, y: point.y },
|
|
p2: Point { x: point.x + dim, y: point.y + dim },
|
|
}
|
|
}
|
|
|
|
fn main() {
|
|
let point: Point = Point { x: 0.3, y: 0.4 };
|
|
|
|
println!("point coordinates: ({}, {})", point.x, point.y);
|
|
|
|
let Point { x: my_x, y: my_y } = point;
|
|
|
|
let _rectangle = Rectangle {
|
|
p1: Point { x: my_y, y: my_x },
|
|
p2: Point { x: point.x, y: point.y },
|
|
};
|
|
|
|
let _nil = Nil;
|
|
|
|
let pair = Pair(1, 0.1);
|
|
|
|
println!("pair contains {:?} and {:?}", pair.0, pair.1);
|
|
|
|
let Pair(integer, decimal) = pair;
|
|
|
|
println!("pair contains {:?} and {:?}", integer, decimal);
|
|
|
|
println!("rect is {:?}", _rectangle);
|
|
println!("rect area is {}", rect_area(_rectangle));
|
|
|
|
let dim = 9.1f32;
|
|
|
|
println!("point is {:?}", point);
|
|
println!("dim is {:?}", dim);
|
|
let sq = square(point, dim);
|
|
println!("square is {:?}", sq);
|
|
println!("square area is {}", rect_area(sq));
|
|
}
|