Intro

Structs have a similiar syntax to a Class and are used to hold data.

crystal
# 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

Considerations

  • 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.