#password #email #type #credentials


Email and Password Type in Rust

8 releases (5 breaking)

0.8.2 Dec 7, 2023
0.7.0 Apr 14, 2023
0.5.0 Mar 29, 2023
0.3.1 Dec 23, 2022
0.1.1 Nov 29, 2022

#282 in Encoding

Download history 56/week @ 2024-02-19 5/week @ 2024-02-26 17/week @ 2024-03-11 92/week @ 2024-04-01

109 downloads per month

MIT license

708 lines



Email and Password types in Rust.

Email data type

use email_pass::Email;
fn main() {
    let email1 = Email::build("john", "example.com").expect("Error creating a email");
    let email2 = Email::from_str("john@example.com").expect("Error with string email");

    assert_eq!(&email1, &email2);

    assert_eq!(email2.username(), "john");
    assert_eq!(email2.domain(), "example.com");

Password data type

The type Password differentiates the raw password from encrypted passwords and provides only the correct methods for each.

use email_pass::Password;
fn main() -> Result<(), Error> {
    let encrypt_password = Password::new("ThisIsAPassPhrase.And.Secure.Password")
        .check()? // raw password method
        .to_encrypt_default()?; // raw password method
    // encrypted passwords implements the Deref trait
    let password = Password::from_encrypt(encrypt_password.as_str())?;
    println!("{}", password);


The next code don't compile, because the raw passwords do not implement either the Display trait or the Debug trait.

use email_pass::Password;
fn main() {
    let password = Password::new("ThisIsAPassPhrase.And.Secure.Password");
    println!("{}", &password); //
    println!("{:?}", &password); //

Legacy Password and Email types

You can use the old types behind the legacy feature.

email_pass = { version = "0.7.0", features = ["legacy"] }


use email_pass::Password;
fn main() {
    let unsafe_password = Password::new("01234".to_string());
    let safe_password = Password::new(


If the password is not encrypted, you can't access the inner value.

use email_pass::password::legacy::Password;
fn main() {
    let mut password = Password::from_raw(

    password.encrypt_password().expect("Error encrypting password");

The Password type implements the Debug trait securely.

fn main(){
    let safe_password = Password::from_raw("ThisIsAPassPhrase.And.Secure.Password".to_string());
    let str_password = format!("{:?}", &safe_password);


You can construct the Email with the new method.

fn main(){
    let correct_email = Email::new("example@example.com");
    let incorrect_email = Email::new("example.com");

Serde Suport

The types Email and Password implements the traits Serialize and Deserialize in the feature serde.

email_pass = { version = "0.7.0", features = ["serde"] }

Migration from version 0.4.1 to version <= 0.7.0

If you don't want break your code, just use the feature legacy:

email_pass = { version = "0.7.0", features = ["legacy"] }

Then, you can try the new Password type with the import:

use email_pass::password::safe::Password;

Migration from version 0.4.1 to version 0.8.0+

Your code must have been broken when upgrading, because the v0.8.0 uses a new errors API, and uses a new Email constructors. To fix your code:

  • Adapt your error types to migrate to the new version.
  • Replace all uses of Deref trait with Password type.
  • Replace all uses of Email::new method with Email::build or Email::from_str.

Migration from version 0.7.0 to version 0.8.0+

Same case as above. But if you use have been using both safe and legacy password types, you should choose only one.


Thanks to letsgetrusty for the repo that inspired the Password type.


~140K SLoC