How to use Structs in Elixir

Structs in Elixir

In Elixir, Structs are maps with reduced functionality, compile-time checks, and default values.

Reduced functionality means that structs cannot use protocols defined for maps like Enum, but can use functions from the Map module.

Compile-time checks ensure that structs do not have any variable that has not been declared for it, and that default values are assigned values to struct member variables if no value has been ​explicitly assigned.

Usage

Structs need to be defined before use:

defmodule City do
defstruct name: "New York City", state: "New York", country: "USA"
end

In this example, a City struct is defined. The struct name follows the defmodule keyword, and the variables and their default values follow the defstruct keyword.

If no default value is assigned to a variable when the struct is defined, and if that variable is not given a value when a struct object is defined, then that variable is given the nil value. All variables without default values need to be declared to the left of variables with default values.

defmodule City do
defstruct [:name , :state, country: "USA"]
end
defmodule City2 do
defstruct name: nil , state: nil, country: "USA"
end

As can be seen above, any variable name without a default value is written like an atomic value, but all variables are in square brackets. Another way, without square brackets, would be to explicitly give these variables a default value of nil. If any of these variables without a default value are moved to the right of country, the program will give an error:

# Defining struct
defmodule City do
defstruct name: "New York City", state: "New York", country: "USA"
end
# Defining struct with some undefined property values
defmodule City2 do
defstruct [:name , :state, country: "USA"]
end
defmodule Main do
def main do
# Defining struct variables of City struct
london = %City{name: "London", state: "Greater London", country: "UK"}
rochester = %City{name: "Rochester"}
newyork = %City{}
# Displaying variables
IO.puts "Displaying struct variable:"
IO.inspect london
IO.puts "Displaying struct variable with a few default values:"
IO.inspect rochester
IO.puts "Displaying struct variable with all default values:"
IO.inspect newyork
# Defining struct variable of City2 struct
losangeles = %City2{}
# Displaying variable
IO.puts "Displaying struct variable with all default values and some without definition:"
IO.inspect losangeles
# Updating values
IO.puts "Changing value of struct properties:"
losangeles = %{losangeles | name: "Los Angeles"}
losangeles = %{losangeles | state: "California"}
IO.inspect losangeles
# Accessing individual struct variable
IO.puts "Accessing individual struct property:"
IO.puts losangeles.name
end
end
Main.main

This example demonstrates how structs are declared, accessed, and changed, and how they behave depending on the presence and absence of default values.

As seen in the example, the syntax to change the value of a struct property is as follows: objectStorageVariable = %{objectToBeChanged | property: newValue}. Here, objectStorageVariable is the variable in which the object with the changed value is to be stored. objectToBeChanged is the object variable that is going to be changed. property is the struct property whose value to be changed. newValue is the new value to be assigned to the property. To access a single struct property, the object.property syntax is followed.

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved