Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use std::fmt::Debug;
- trait Shape {
- fn area(&self) -> f32;
- }
- #[derive(Debug)]
- struct Square {
- length: i32,
- }
- impl Square {
- fn new(length: i32) -> Square {
- Square { length }
- }
- }
- impl Shape for Square {
- fn area(&self) -> f32 {
- (self.length * self.length) as f32
- }
- }
- #[derive(Debug)]
- struct Rectangle {
- width: i32,
- height: i32,
- }
- impl Rectangle {
- fn new(width: i32, height: i32) -> Rectangle {
- Rectangle { width, height}
- }
- }
- impl Shape for Rectangle {
- fn area(&self) -> f32 {
- (self.width * self.height) as f32
- }
- }
- fn print_area<S: Shape+Debug>(shape: &S) {
- println!("The area of {:?} is {}",
- shape,
- shape.area());
- }
- fn main() {
- let rect = Rectangle::new(30, 50);
- let sqr = Square::new(30);
- print_area(&rect);
- print_area(&sqr);
- }
Add Comment
Please, Sign In to add comment