Search⌘ K
AI Features

Maps and Structs

Explore the similarities and differences between maps and structs in Elixir, discover how structs are implemented as maps, and understand the use of the @enforce_keys attribute to enforce required fields for safer data handling.

Introduction

The map has rapidly become the go-to data structure for Elixir programmers. For this lesson, we’re going to treat maps and structs as basically the same thing. In iex, we can see that a struct is implemented as a map. Let’s take a peek under the hood by running the following commands in the iex terminal:

Executable

C++
defmodule User do
defstruct [:name, :email]
end
C++
map = %User{}
C++
is_map(map)
C++
map.__struct__

Output

iex(1)> defmodule User
...