How to Implement Traits for External Types (Newtype Pattern)

Wrap the external type in a new struct to implement traits on it without modifying the original type.

You implement traits for external types by wrapping them in a new struct (the Newtype Pattern) and implementing the trait on your wrapper.

struct MyWrapper(Vec<i32>);

impl MyWrapper {
    fn new() -> Self {
        MyWrapper(vec![1, 2, 3])
    }
}

impl std::fmt::Display for MyWrapper {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "MyWrapper({:?})", self.0)
    }
}

This works because MyWrapper is a distinct type from Vec<i32>, allowing you to define custom behavior for it without modifying the original type.