面试题答案
一键面试// 定义Rectangle结构体
struct Rectangle {
width: u32,
height: u32,
}
// 为Rectangle结构体实现area方法
impl Rectangle {
fn area(&self) -> u32 {
self.width * self.height
}
}
fn main() {
// 创建Rectangle实例
let rect1 = Rectangle { width: 30, height: 50 };
// 调用area方法并打印结果
println!(
"The area of the rectangle is {} square pixels.",
rect1.area()
);
}