Struct: Difference between revisions
From Coderwiki
More actions
new: info, example, other algebraic |
(No difference)
|
Latest revision as of 08:36, 15 August 2025
A struct is a user-defined data type.
It is a product type - a data type made by combining multiple fields together into a single type.
Example: a 'Rectangle' struct
edit edit source
As structs store multiple different fields in a single collection, we can make a Rectangle struct like this (written in Rust for this example):
struct Rectangle {
width: u32,
height: u32,
color: Color,
}
And our Color struct could look like this, another product type:
struct Color {
red: u8,
green: u8,
blue: u8,
alpha: u8,
}
Other algebraic data types
edit edit sourcePlease see: Algebraic data type