Skip to content

Instantly share code, notes, and snippets.

@metatoaster
Last active May 26, 2023 23:58
Show Gist options
  • Save metatoaster/18a4fe8b7137bc1d94aae3731cc75e6b to your computer and use it in GitHub Desktop.
Save metatoaster/18a4fe8b7137bc1d94aae3731cc75e6b to your computer and use it in GitHub Desktop.
#[derive(Debug)]
struct EmailAddr(String); // e.g. user@example.com
#[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("user@example.com".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