Blanket Implementations

"Implementations of a trait on any type that satisfies the trait bounds are called blanket implementations." -- The Book

use std::fmt::{Display, Formatter, Result};

struct Point<T> {
    x: T,
    y: T,
}

impl<T> Point<T> {
    fn new(x: T, y: T) -> Self {
        Self { x, y }
    }
}

impl<T: Display> Display for Point<T> {
    fn fmt(&self, f: &mut Formatter) -> Result {
        write!(f, "x : {}, y : {}", self.x, self.y)
    }
}

fn main() {
    let p = Point::new(1.1, 2.2);
    let pstr = p.to_string();
    println!("{pstr}");
}

let pstr = p.to_string(); works because of the following blanket implementation in Rust Standard library.


#![allow(unused)]
fn main() {
impl<T : Display> ToString for T { ... }
}

ToString trait is implemented in Standard library for any type that satisfies Display trait.