Semigroup

Trait Semigroup 

Source
pub trait Semigroup: Sized {
    // Required method
    fn combine(&self, other: &Self) -> Self;

    // Provided method
    fn combine_assign(&mut self, other: &Self) { ... }
}
Expand description

A semigroup: a type with an associative binary operation.

Laws (not enforced by type system):

  • Associative: a.combine(b).combine(c) == a.combine(b.combine(c))

§Example

use algebra::Semigroup;

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
struct Max(i32);

impl Semigroup for Max {
    fn combine(&self, other: &Self) -> Self {
        Max(self.0.max(other.0))
    }
}

let x = Max(3);
let y = Max(5);
let z = Max(2);
assert_eq!(x.combine(&y).combine(&z), x.combine(&y.combine(&z)));

Required Methods§

Source

fn combine(&self, other: &Self) -> Self

Combine two elements associatively.

Provided Methods§

Source

fn combine_assign(&mut self, other: &Self)

In-place combine.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§