January 2019
Intermediate to advanced
520 pages
14h 32m
English
We'll extend our random-number-generating service with two features – generating random colors and shuffling an array of bytes. For the first feature, we need to add the Color struct to hold the color components. Create a new file, color.rs, and add the following code to it:
#[derive(Clone, PartialEq, Eq)]pub struct Color { pub red: u8, pub green: u8, pub blue: u8,}
Add two constant colors that we'll use later:
pub const WHITE: Color = Color { red: 0xFF, green: 0xFF, blue: 0xFF };pub const BLACK: Color = Color { red: 0x00, green: 0x00, blue: 0x00 };
The struct also implements PartialEq and Eq to compare a value with these constants.
We'll use a textual representation of color that's compatible with CSS. We'll support ...
Read now
Unlock full access