# Type equality in stable Rust. ## Use A toy example that demonstrates the basics: ```rust use same_as::SameAs; trait Eat { fn eat>(_: U); } // Please don't actually write this struct MrCreosote; impl Eat for MrCreosote { fn eat>(_: U) {} } MrCreosote::eat(0_u8); // wafer-thin type ``` This won't compile: ```rust // ... struct MrCreosote; impl Eat for MrCreosote { fn eat>(_: U) {} } MrCreosote::eat(0_u16); // kaboom ``` ## But why is type equality necessary? Sometimes you need it where Rust can't leverage it now, e.g. defining a Haskell-style monad in Rust: ```rust pub trait Monad: SameAs> { // <-- Enforces that e.g. for `Maybe`, `Self::Constructor` is effectively just the type constructor `Maybe`. type Constructor: Monad; // In this `impl`, `Self` is really `Self`, but we want to make `Self` below. fn bind B>(self, f: F) -> Self::Constructor; } ``` So this would work: ```rust pub enum Maybe { Nothing, Just(A) } impl Monad for Maybe { type Constructor = Maybe; } ``` but we can prove that this won't, and so we can safely simulate type constructors in Rust: ```rust pub enum Maybe { Nothing, Just(A) } // deception! vvvvvv impl Monad for Maybe { type Constructor = Option; } ```