Crystal Notes: Structs
Published: 4th of August 2021
Structs have a similiar syntax to a Class and are used to hold data.
# Define an Struct. struct Stuff @stuff : String property stuff def initialize(@stuff) end def print puts(@stuff) end end # Instatiate the Stuff struct. s = Stuff.new("my stuff") # Use the print method. s.print #=> my stuff
- Structs are created on the stack and are passed by value.
- Structs are best suited for small amounts of immutible data.
- Structs can be mutible, since values are copied, the behaviour is different from a class.