Square extension on numbers
mod foo {
pub trait SquareExt {
type Out; // Associated type
fn square(self) -> Self::Out;
}
use std::ops::Mul;
impl<T> SquareExt for T
where
T: Mul + Copy,
{
type Out = <T as Mul>::Output; // Using Mul's associated type
fn square(self) -> Self::Out {
self * self
}
}
}
fn main() {
use foo::SquareExt;
let a = 10.square();
}
Top comments (0)