0

Rust – Validate Email Address using Regular Expressions

Last Updated on

This post is about validating email addresses in Rust using the crate regex and regular expressions based on this other post.

Requirements

  • Cargo
  • A Rust crate called regex

Regular Expressions in Rust

An email address contains two (2) parts – username and domain name. These are combined using the ‘@’ symbol, e.g., karl@abc.com. Let us create regular expressions for each for now.

Username

Output

Domain

Output

Username + @ + Domain

To create a regular expression to validate email addresses, we combine the aforementioned expressions.

Test

We have these email addresses for testing.

Check each email address

Output

Complete Codes

Cargo.toml

main.rs

Tested with Rust 1.37.0.

Avatar

Karl San Gabriel

Professional Software Developer