# Structs

Structs are immutable data structures that map keys to values. They are semantically similar to tables, but are immutable. They also can be used as keys in tables and other structs, and follow expected equality semantics. Like tables, they are backed by an efficient, native hash table.

To create a struct, you may use a struct literal or the `struct` function.

``````(def my-struct {:key1 2
:key2 4})

(def my-struct2 (struct
:key1 2
:key2 4))

# Structs with identical contents are equal
(= my-struct my-struct2) #-> true``````

As with other data structures, you may retrieve values in a struct via the `get` function, or call the struct as a function. Since structs are immutable, you cannot add key-value pairs

``````(def st {:a 1 :b 2})

(get st :a) #-> 1
(st :b) #-> 2``````

## Converting a table to a struct

A table can be converted to a struct via the `table/to-struct` function. This is useful for efficiently creating a struct by first incrementally putting key-value pairs in a table.

``````(def accum @{})
(for i 0 100
(put accum (string i) i))
(def my-struct (table/to-struct accum))``````

## Using structs as keys

Because a struct is equal to any struct with the same contents, structs make useful keys for tables or other structs. For example, we can use structs to represent Cartesian points and keep a mapping from points to other values.

``````(def points @{
{:x 10 :y 12} "A"
{:x 12 :y 10} "B"
{:x 0 :y 0}   "C"})

(defn get-item
"Get item at a specific point"
[x y]
(points {:x x :y y}))

(get-item 10 12) # -> "A"
(get-item 0 0) # -> "C"
(get-item 5 5) # -> nil``````