1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
pub trait Semigroup { fn combine(self, other: Self) -> Self; } macro_rules! semigroup_numeric_impl { ($($t:ty)*) => ($( impl Semigroup for $t { fn combine(self, other: Self) -> Self { self + other } } )*) } semigroup_numeric_impl! { usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 f32 f64 } impl<T: Clone> Semigroup for Vec<T> { fn combine(self, other: Self) -> Self { let mut concat: Vec<T> = self.to_vec(); concat.extend_from_slice(&other); concat } } impl Semigroup for String { fn combine(self, other: Self) -> Self { format!("{}{}", self, other) } }