This post shows how to validate email addresses in Rust using Regular Expressions. Although Rust does not have a standard library for regular expressions, we could use the crate regex and regular expressions on this other post.
Requirements To Validate Email
- Rust 1.52.1
- A Rust crate called regex
Regular Expressions To Validate Email 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.
For username, we can have the following regular expressions and sample codes.
1 2 3 4 | let username_regex = Regex::new(r"[a-z0-9_+]([a-z0-9_+.]*[a-z0-9_+])?").unwrap(); let username = "karl"; let is_username_valid = username_regex.is_match("karl"); println!("{} is valid? {} ", username, is_username_valid); |
When we run these codes, we get the following output.
1 | karl is valid? true |
For the domain part, we can use the following expressions with some codes for testing.
1 2 3 4 | let domain_regex = Regex::new(r"^[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,6}$").unwrap(); let domain = "xyz/com"; let is_domain_valid = domain_regex.is_match(domain); println!("{} is valid? {}", domain, is_domain_valid); |
The output of these codes is as follows.
1 | xyz/com is valid? false |
Then, we would have the following regular expressions if we combine username, @, and domain. Notice the @ symbol sits between the two regular expressions we tested before.
1 | let email_regex = Regex::new(r"^([a-z0-9_+]([a-z0-9_+.]*[a-z0-9_+])?)@([a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,6})").unwrap(); |
Test Rust Codes To Validate Email Addresses
We use some emails to validate against the regular expressions in a loop to test the combined expressions. Meaning, given the list of email addresses, we validate each using our regular expressions. Consider the following list of email addresses for testing.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | let email_addresses = [ "foo@bar.com", "foo at bar.com", "foo.bar42@c.com", "42@c.com", "f@42.co", "foo@4-2.team", ".x@c.com", "x.@c.com", "foo_bar@bar.com", "_bar@bar.com", "foo_@bar.com", "foo+bar@bar.com", "+bar@bar.com", "foo+@bar.com", "foo.lastname@bar.com" ]; |
As we can see, some emails use dots, +, underscores, etc. The following codes validate each email address in a loop. We use the is_match function with each email as the function’s argument.
1 2 3 | for email_address in &email_addresses { println!("{} is valid: {} ", email_address, email_regex.is_match(email_address)) } |
When we run the codes, we get the following results.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | foo@bar.com is valid: true foo at bar.com is valid: false foo.bar42@c.com is valid: true 42@c.com is valid: true f@42.co is valid: true foo@4-2.team is valid: true .x@c.com is valid: false x.@c.com is valid: false foo_bar@bar.com is valid: true _bar@bar.com is valid: true foo_@bar.com is valid: true foo+bar@bar.com is valid: true +bar@bar.com is valid: true foo+@bar.com is valid: true foo.lastname@bar.com is valid: true |
When using the crate regex in Rust to validate email addresses using regular expressions, we include its dependency in the Cargo.toml file. We only need one crate for regular expressions for our Rust codes.
1 2 3 4 5 6 7 8 | [package] name = "rust-project1" version = "0.1.0" authors = ["Karl San Gabriel"] edition = "2018" [dependencies] regex = "1.3.1" |
Finally, the complete codes look as follows.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | extern crate regex; use regex::Regex; fn main() { let email_regex = Regex::new(r"^([a-z0-9_+]([a-z0-9_+.]*[a-z0-9_+])?)@([a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,6})").unwrap(); let email_addresses = ["foo@bar.com", "foo at bar.com", "foo.bar42@c.com", "42@c.com", "f@42.co", "foo@4-2.team", ".x@c.com", "x.@c.com", "foo_bar@bar.com", "_bar@bar.com", "foo_@bar.com", "foo+bar@bar.com", "+bar@bar.com", "foo+@bar.com", "foo.lastname@bar.com"]; for email_address in &email_addresses { println!("{} is valid: {} ", email_address, email_regex.is_match(email_address)) } } |
To use the crate regex in our Rust codes, we use use regex::regex. Then, we create an instance of the Regex struct. Next, use that instance to validate an email address using a regular expression via the is_match function.