Skip to content

Commit

Permalink
feat: add hello-derive-more
Browse files Browse the repository at this point in the history
  • Loading branch information
katopz committed Sep 4, 2024
1 parent 988f8c7 commit e83da4d
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 0 deletions.
1 change: 1 addition & 0 deletions examples/r3/hello-derive-more/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
target
72 changes: 72 additions & 0 deletions examples/r3/hello-derive-more/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions examples/r3/hello-derive-more/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "hello-derive-more"
version = "0.1.0"
edition = "2021"

[dependencies]
derive_more = { version = "1.0.0", features = ["add", "display", "from", "from_str"] }
23 changes: 23 additions & 0 deletions examples/r3/hello-derive-more/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
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);
}

0 comments on commit e83da4d

Please sign in to comment.