Consts in Structs


#1
  1. I currently have:

#[derive(Copy, Clone, Debug)]
#[repr(C)]
pub struct Color3F32 {
    pub r: f32,
    pub g: f32,
    pub b: f32,
}

impl Color3F32 {
    pub fn black() ->  Color3F32 { Color3F32 { r: 0.0, g: 0.0, b: 0.0 } }
    pub fn white() ->  Color3F32 { Color3F32 { r: 1.0, g: 1.0, b: 1.0 } }
    pub fn red() ->    Color3F32 { Color3F32 { r: 1.0, g: 0.0, b: 0.0 } }
    pub fn green() ->  Color3F32 { Color3F32 { r: 0.0, g: 1.0, b: 0.0 } }
    pub fn blue() ->   Color3F32 { Color3F32 { r: 0.0, g: 0.0, b: 1.0 } }
}

  1. I don’t like the fact it’s a function call. Is there a way to make it “class constants” instead of “functions” ?

#2

Just make them consts; constructors (as in Color3F32 { /*...*/ }) are constant as long as their input is constant.
(Playground)


#3

Very nice; thanks! Turns out we can put the “consts” inside a “impl Color3F32” too.