Rust methods: Difference between revisions

From wikinotes
(Created page with "Rust lets you add methods to structs. {{ TODO | other types? what's the full spectrum here? }} = Structs = <blockquote> Rust lets you add methods to structs <syntaxhighlight lang="rust"> type RgbColor{ r: u8, g: u8, b: u8, } impl RgbColor { fn brighten(&self) { self.r += 8; self.g += 8; self.b += 8; } } let c = RgbColor{r: 8, g: 8, b: 8}; c.brighen(); // {r: 16, g: 16, b: 16} </syntaxhighlight> </blockquote><!-- Structs -->")
 
No edit summary
Line 2: Line 2:


{{ TODO
{{ TODO
| other types? what's the full spectrum here? }}
| other types? what's the full spectrum here?  
}}


= Structs =
= Structs =

Revision as of 19:47, 7 February 2023

Rust lets you add methods to structs.

TODO:

other types? what's the full spectrum here? 

Structs

Rust lets you add methods to structs

type RgbColor{
    r: u8,
    g: u8,
    b: u8,
}

impl RgbColor {
    fn brighten(&self) {
        self.r += 8;
        self.g += 8;
        self.b += 8;
    }
}

let c = RgbColor{r: 8, g: 8, b: 8};
c.brighen();  // {r: 16, g: 16, b: 16}