Module derive_more
Derive implementations for common traits.
- Crate
::derive_more. - docs.rs
- crates.io
- GitHub
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
- derive Module containing derive definitions only, without their corresponding traits.
- with_trait Module containing derive definitions with their corresponding traits along.
Structs
- FromStrError Error of parsing an enum value its string representation.
-
TryFromReprError
Error returned by the derived
TryFromimplementation on enums to convert from their repr. -
TryIntoError
Error returned by the derived
TryIntoimplementation. -
TryUnwrapError
Error returned by the derived
TryUnwrapimplementation. - UnitError Error returned by the derived implementations when an arithmetic or logic operation is invoked on a unit-like variant of an enum.
- WrongVariantError Error returned by the derived implementations when an arithmetic or logic operation is invoked on mismatched enum variants.
Enums
- BinaryError Possible errors returned by the derived implementations of binary arithmetic or logic operations.