Module derive_more

Derive implementations for common traits.


derive_more extends Rust's derive macro system to automatically implement commonly-used traits for custom types.

When wrapping types inside custom structs or enums, the implementations of built-in traits like Add, Display, and From are lost. derive_more provides derives that restore these implementations with minimal boilerplate, making the newtype pattern ergonomic.

The crate provides derives across several categories: conversion traits (From, Into, TryFrom, IntoIterator), formatting traits (Display and related), operator overloading (Add, Mul, Deref), and enum utilities that generate helper methods like is_variant().

Key features include automatic operator implementations for wrapper types, flexible Display formatting with format strings, and comprehensive support for both structs and enums.

Examples

Deriving arithmetic operators for a newtype:

use derive_more::{Add, From};

#[derive(Add, From, Debug, PartialEq)]
struct MyInt(i32);

let a = MyInt(10);
let b = MyInt(20);
let result = a + b;
assert_eq!(result, MyInt(30));

Deriving Display with custom formatting:

use derive_more::Display;

#[derive(Display, Debug)]
#[display("Point({x}, {y})")]
struct Point {
    x: i32,
    y: i32,
}

let p = Point { x: 10, y: 20 };
assert_eq!(format!("{}", p), "Point(10, 20)");

Modules