DEV Community

Ryan Will
Ryan Will

Posted on Originally published at til.ryanwill.dev on

Using pattern matching to compare two strings

Elixir’s pattern matching feature is the gift that keeps on giving. I love this neat trick to check if two strings are the same. It is an easy way to check if a password and password confirmation match.

def passwords_match?(password, password), do: true

def passwords_match?(_, _), do: false

passwords_match?("password", "password") # true
passwords_match?("password", "notpassword") # false

Top comments (0)