Last active
May 26, 2023 23:58
-
-
Save metatoaster/18a4fe8b7137bc1d94aae3731cc75e6b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#[derive(Debug)] | |
struct EmailAddr(String); // e.g. [email protected] | |
#[derive(Debug)] | |
struct Domain<'a>(&'a str); // a slice onward from the @ symbol of the above | |
impl<'a> From<&'a EmailAddr> for Domain<'a> { | |
fn from(value: &'a EmailAddr) -> Self { | |
Self(&value.0[value.0.find('@').unwrap_or(0)..]) | |
} | |
} | |
fn main() { | |
let email = EmailAddr("[email protected]".to_string()); | |
let domain = Domain::from(&email); | |
println!("domain for {:?} is {:?}", email, domain); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment