pub trait Monoid<'a>: Semigroup<'a> {
// Required method
fn empty() -> Apply0<Self>;
}
Expand description
A typeclass for monoids.
Monoid
extends Semigroup
with an identity element. A monoid is a set
equipped with an associative binary operation and an identity element.
In functional programming, monoids are useful for combining values in a consistent way, especially when accumulating results or folding collections.
§Laws
Monoid instances must satisfy the following laws:
- Left identity:
append(empty(), x) = x
. - Right identity:
append(x, empty()) = x
. - Associativity:
append(append(x, y), z) = append(x, append(y, z))
.
§Examples
Common monoids include:
- Strings with concatenation and empty string.
- Numbers with addition and zero.
- Numbers with multiplication and one.
- Lists with concatenation and empty list.
Required Methods§
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.