当先锋百科网

首页 1 2 3 4 5 6 7
// lib.rs 文件内容

pub mod shape {
    pub trait Area {
        fn calculate_area(&self) -> u32;
    }
    #[derive(Debug)]
    pub struct Rectangle {
        width: u32,
        height: u32,
    }

    impl Rectangle {
        pub fn set_w_h(self: &mut Rectangle, w: u32, h: u32) {
            self.width = w;
            self.height = h;
        }

        pub fn new() -> Rectangle {
            Rectangle {
                width: 0,
                height: 0,
            }
        }
    }

    impl Area for Rectangle {
        fn calculate_area(&self) -> u32 {
            self.height * self.width
        }
    }
    #[derive(Debug)]
    pub struct Circle {
        radius: u32,
    }

    impl Circle {
        pub fn set_radius(self: &mut Circle, val: u32) {
            self.radius = val;
        }

        pub fn new() -> Circle {
            Circle { radius: 0 }
        }
    }

    impl Area for Circle {
        fn calculate_area(&self) -> u32 {
            self.radius * self.radius
        }
    }
}

// main.rs

use string_test::shape::{self, Area};
fn main() {
    let mut a = shape::Circle::new();
    a.set_radius(10);
    let a = a.calculate_area(); 
    println!("circleArea: {}", a);

    let mut b = shape::Rectangle::new();
    b.set_w_h(19, 18);
    println!("rectangleArea: {}", b.calculate_area());
}