Derive More

🤔 Refer to The Newtype Pattern In Rust

Do

  • Implement From not Into.
  • Use TryFrom for validation + From.
  • Implementing Newtype Traits with Derive More.

Setup

cargo add derive_more --features "add display from from_str"

Code

use derive_more::{Add, Display, From, FromStr};

#[derive(FromStr, Display)]
pub struct PhoneNumber(String);

#[derive(Clone, Copy, From, Display, Add)]
#[display("{} years", _0)]
pub struct Years(u32);

fn main() {
    // Use parse.
    let num = "123-4567".parse::<PhoneNumber>().unwrap();
    println!("1️⃣ Phone number is {}", num);

    // Use from_str.
    let num = PhoneNumber::from_str("123-4567").unwrap();
    println!("2️⃣ Phone number is {}", num);

    // Use from and Add.
    let age_1 = Years::from(5);
    let age_2 = Years::from(2);
    println!("3️⃣ {} + {} = {}", age_1, age_2, age_1 + age_2);
}
[package]
name = "hello-derive-more"
version = "0.1.0"
edition = "2021"

[dependencies]
derive_more = { version = "1.0.0", features = ["add", "display", "from", "from_str"] }

Output

1️⃣ Phone number is 123-4567
2️⃣ Phone number is 123-4567
3️⃣ 5 years + 2 years = 7 years