# Why does \`elem/2\` exist?

**URL:** <https://forum.elixirforum.com/t/why-does-elem-2-exist/64854>\
**Category:** Questions\
**Tags:** functions\
**Created:** [July 11, 2024, 3:04am UTC](https://forum.elixirforum.com/t/why-does-elem-2-exist/64854 "2024-07-11T03:04:54Z")\
**Posts on this page:** 1\
**Showing post:** 5

<div class="post-metadata">

**Author:** ![heilong](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/heilong/32/35976_2.png) [@heilong](https://forum.elixirforum.com/u/heilong)\
**Post date:** [July 11, 2024, 6:59am UTC](https://forum.elixirforum.com/t/why-does-elem-2-exist/64854/5 "2024-07-11T06:59:03Z")

</div>

`elem/2` can be used in guards, pattern matching cannot

`elem/2` is O(1) access to a vector (the only other O(1) example is binaries).

Pattern matching with large tuples is verbose and it’s easy to get the wrong element (why count wildcard matches when you can write the correct index into a function argument).

In the future, it seems we will get O(1) _update_ of tuples in contexts where the compiler knows there are no shared references. So maybe time to incorporate `:erlang.setelement/3` into Elixir somehow.

---

_[View the full topic](https://forum.elixirforum.com/t/why-does-elem-2-exist/64854)._
