Pattern Matching Basics

Elixir doesn’t have a normal assignment operator. In most languages, the = operator is used for simple left = "value" statements. In Elixir, this operator is called the match operator, and it initiates pattern matching.

Pattern matching is implemented by the BEAM, so it’s baked into the runtime of the language. Some optimizations make it efficient even for a large number of pattern clauses. So, you can use pattern matching without worrying about a negative performance impact on your application.

In this section, we’ll go over different pattern-matching syntaxes for basic data types, lists, maps, tuples, and more.

Match Basic Types

Let’s start with the most basic syntax for pattern matching. Open a new IEx session and ...

Get From Ruby to Elixir now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.