Rust Standard Traits Reference

Key Rust std traits with required methods, blanket impls and derive support.

Searchable reference for core Rust standard-library traits — Debug, Display, Clone, Copy, PartialEq, Eq, Ord, Hash, Default, Iterator, From, Into, Deref, Drop — with required methods, derive support, and blanket-impl notes.

What is the difference between Clone and Copy?

Clone provides an explicit clone method for a potentially expensive deep copy. Copy marks a type as cheaply duplicable by a simple bitwise copy on assignment, with no method to call. Copy requires Clone, and only types whose fields are all Copy can be Copy.

Rust’s standard library defines a set of traits that types opt into to gain shared behaviour — formatting, cloning, comparison, iteration, conversion. Many can be derived; others must be written by hand. This tool is a searchable reference covering each trait’s required methods, derive support, and blanket implementations.

How it works

A trait declares method signatures; an impl Trait for Type block supplies them. Some methods are required (you must provide them) and some are provided (default implementations you may override). The reference notes, for each trait:

  • Required methods — the minimum you must implement.
  • Derivable — whether #[derive(...)] can generate the impl when all fields cooperate.
  • Blanket impls — automatic implementations, such as Into from From, or ToString from Display.

Worked example

#[derive(Debug, Clone, PartialEq, Eq, Hash, Default)]
struct Point { x: i32, y: i32 }

// Display cannot be derived — write it by hand:
use std::fmt;
impl fmt::Display for Point {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "({}, {})", self.x, self.y)
    }
}

// From gives Into for free via a blanket impl:
impl From<(i32, i32)> for Point {
    fn from((x, y): (i32, i32)) -> Self { Point { x, y } }
}
let p: Point = (1, 2).into(); // Into<Point> exists automatically

Notes

  • Implementing Display automatically gives you ToString (and thus .to_string()) through a blanket impl — never implement ToString directly.
  • Eq, Ord, and Hash are consistency-marker contracts: deriving them keeps ==, ordering, and hashing in agreement, which HashMap/BTreeMap rely on.
  • Drop cannot be derived and cannot be called manually; the compiler invokes it when a value goes out of scope. Use std::mem::drop to drop early.
  • Deref powers smart pointers like Box and Rc; abusing it for inheritance is discouraged.