Rust 结构体方法

struct  Rectangle {
    width: u32,
    length: u32,
}
impl Rectangle {
    fn area(&self) -> u32 {
        self.width*self.length
    }
}
fn main() {
    let rect = &Rectangle {
        width: 30,
        length: 50,
    };
    println!("The Area is {}", rect.area())
}


猜你喜欢

转载自blog.csdn.net/qq2942713658/article/details/114734945