Rust ---- trait和mod的使用

本文详细探讨了Rust编程语言中的trait和mod的使用。通过学习,读者将理解如何利用trait实现泛型编程,以及如何通过mod进行模块化组织代码,提升代码的可读性和复用性。
摘要由CSDN通过智能技术生成
// 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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值