Common Traits & Derive in Rust
From the Rust cheat sheet · Traits · verified Jul 2026
Common Traits & Derive
Derive traits automatically and implement From/Into for conversions.
rust
// Derive common traits
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
struct Point { x: i32, y: i32 }
// From/Into — type conversions
impl From<(i32, i32)> for Point {
fn from((x, y): (i32, i32)) -> Self {
Point { x, y }
}
}
let p: Point = (10, 20).into();
// Display — custom printing with {}
impl std::fmt::Display for Point {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "({}, {})", self.x, self.y)
}
}💡 Implementing From<T> gives you Into<T> for free — always implement From, not Into
⚡ #[derive(Default)] gives you Config::default() — all fields use their type's default (0, "", false)
📌 Copy can only be derived if all fields are Copy — String is not Copy, so structs with String can't be
🟢 Operator overloading uses traits from std::ops — Add for +, Sub for -, Mul for *, Index for []
derivefromintodisplayoperator-overloading