In Elixir pattern matching can be done using = operator, the equal sign compares left-hand side variable with right-hand side variable, it’s success if both are equal. Lets see simple example:
'bar' = foo is valid because both left & right side are equal to ‘bar’. But in the line 3, when the sides don’t match it’s raise MatchError. From above example, variable can only be assigned on the left side.
A pattern (the left side) is matched if the values (the right side) have the same structure and if each term in the pattern can be matched to the corresponding term in the values.
Also, you can ignoring a value when assignment with underscore operator ‘_’. It’s useful when we didn’t need to capture a value during assignment.
Another feature that exist in elixir pattern matching is caret ^ operator. It’s useful if you want to force to Elixir to use existing value or you don’t want to rebinding a variable. Just prefix a variable with ^ to use existing value.