DEV Community

Ryan Will
Ryan Will

Posted on • Originally published at til.ryanwill.dev on

7 1

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)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay