// 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
Rust ---- trait和mod的使用
最新推荐文章于 2024-05-13 15:34:28 发布
本文详细探讨了Rust编程语言中的trait和mod的使用。通过学习,读者将理解如何利用trait实现泛型编程,以及如何通过mod进行模块化组织代码,提升代码的可读性和复用性。
摘要由CSDN通过智能技术生成