t_wの輪郭

Feedlyでフォローするボタン

関連関数

2022/2/18 1:50:00

implブロック内のselfを引数に取らない関数。
Javastaticメソッドっぽさがある。

struct Rectangle {width:f64, height:f64}

impl Rectangle {
    fn square(size: f64) -> Rectangle {                             //関連関数
        Rectangle { width: size, height: size }
    }
}

fn main() {
    let rect = Rectangle::square(32.);
    println!("width: {}, height: {}", rect.width, rect.height);     //width: 32, height: 32
}