面试题答案
一键面试struct Rectangle {
width: u32,
height: u32,
}
impl Rectangle {
// 创建Rectangle实例的关联函数
fn new(width: u32, height: u32) -> Self {
Self { width, height }
}
// 关联类型
type AreaType = u32;
// 计算矩形面积的关联函数
fn area(&self) -> Self::AreaType {
self.width * self.height
}
}