Rust: Struct
Status: Courses
- 3 forms of
struct- unit struct
- tuple struct
- C-like struct
unit struct
- Zero sized struct.
- Typically used to model errors or states with no data.
struct Dummy; fn main() { // value is zero sized struct Dummy. let value = Dummy; }
tuple struct
- Fields are not named but are referred by their positions
// Tuple struct struct Color(u8,u8,u8) fn main() { // Define while are 255,255,255 let white = Color(255,255,255) // Get the individual values using their positions let white_r = white.0 let white_g = white.1 let white_b = white.2 // You can deconstruct the RGB value of white by using the syntax below let Color(whiteR, whiteG, whiteB) = white; // You can ignore one field using _ let Color(r, _, b) = white; // Green value is ignored }
- Fields have to be initialised in the order as defined.
- Typically used to model data that has 4-5 attributes
C-like structs
// C-like struct definition struct Player { name: String, iq: u8, friends: u8, score: u16 } fn main() { let name = "Alice".to_string(); // C-like struct declaration let mut player1 = Player { name, iq: 90, friends: 0, score: 1129 } // Changing values in C-like struct player1.score = 1130 }
- fields can be initialised in any order in C-like struct.