pub struct EmailAddress(_);
Expand description

Type representing a single email address. This is basically a wrapper around a String, the email address is parsed for correctness with FromStr::from_str, which is the only want to create an instance. The various components of the email are not parsed out to be accessible independently.

Implementations§

Creates an EmailAddress without checking if the email is valid. Only call this method if the address is known to be valid.

use std::str::FromStr;
use email_address::EmailAddress;

let unchecked = "john.doe@example.com";
let email = EmailAddress::from_str(unchecked).expect("email is not valid");
let valid_email = String::from(email);
let email = EmailAddress::new_unchecked(valid_email);

assert_eq!("John Doe <john.doe@example.com>", email.to_display("John Doe"));

Determine whether the address string is a valid email address. Note this is equivalent to the following:

use email_address::*;
use std::str::FromStr;

let is_valid = EmailAddress::from_str("johnstonskj@gmail.com").is_ok();

Determine whether the part string would be a valid local-part if it were in an email address.

Determine whether the part string would be a valid domain if it were in an email address.

Return this email address formatted as a URI. This will also URI-encode the email address itself. So, name@example.org becomes mailto:name@example.org.

use email_address::*;
use std::str::FromStr;

assert_eq!(
    EmailAddress::from_str("name@example.org").unwrap().to_uri(),
    String::from("mailto:name@example.org")
);

Return a string formatted as a display email with the user name. This is commonly used in email headers and other locations where a display name is associated with the address.

use email_address::*;
use std::str::FromStr;

assert_eq!(
    EmailAddress::from_str("name@example.org").unwrap().to_display("My Name"),
    String::from("My Name <name@example.org>")
);

Returns the local part of the email address. This is borrowed so that no additional allocation is required.

use email_address::*;
use std::str::FromStr;

assert_eq!(
    EmailAddress::from_str("name@example.org").unwrap().local_part(),
    String::from("name")
);

Returns the domain of the email address. This is borrowed so that no additional allocation is required.

use email_address::*;
use std::str::FromStr;

assert_eq!(
    EmailAddress::from_str("name@example.org").unwrap().domain(),
    String::from("example.org")
);

Returns the email address as a string reference.

Trait Implementations§

Converts this type into a shared reference of the (usually inferred) input type.
Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Formats the value using the given formatter. Read more
Deserialize this value from the given Serde deserializer. Read more
Formats the value using the given formatter. Read more
The associated error which can be returned from parsing.
Parses a string s to return a value of this type. Read more
Feeds this value into the given Hasher. Read more
Feeds a slice of this type into the given Hasher. Read more
This method tests for self and other values to be equal, and is used by ==. Read more
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason. Read more
Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
The error type produced by a failed conversion.
Convert the given value into an approximately equivalent representation.
The error type produced by a failed conversion.
Convert the subject into an approximately equivalent representation.
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more
Approximate the subject with the default scheme.
Approximate the subject with a specific scheme.
Approximate the subject to a given type with the default scheme.
Approximate the subject to a given type with a specific scheme.
Convert the subject to a given type.
Attempt to convert the subject to a given type.
Attempt a value conversion of the subject to a given type.

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The alignment of pointer.
The type for initializers.
Initializes a with the given initializer. Read more
Dereferences the given pointer. Read more
Mutably dereferences the given pointer. Read more
Drops the object pointed to by the given pointer. Read more
Should always be Self
Sets value as a parameter of self.
The inverse inclusion map: attempts to construct self from the equivalent element of its superset. Read more
Checks if self is actually part of its subset T (and can be converted to it).
Use with care! Same as self.to_subset but without any property checks. Always succeeds.
The inclusion map: converts self to the equivalent element of its superset.
The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
Converts the given value to a String. Read more
The error type produced by a failed conversion.
Convert the given value into the subject type.
The type returned in the event of a conversion error.
Performs the conversion.
The error type produced by a failed conversion.
Convert the subject into the destination type.
The type returned in the event of a conversion error.
Performs the conversion.
The error type produced by a failed conversion.
Convert the given value into an exactly equivalent representation.
The error type produced by a failed conversion.
Convert the subject into an exactly equivalent representation.