pub trait Semigroup {
// Required method
fn combine(self, rhs: Self) -> Self;
}
Expand description
The trait combines two types into another one.
The combining of several types must be associative, meaning that they can be evaluated in any order.
first.combine(second.combine(third)) == first.combine(second).combine(third)
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.
Implementations on Foreign Types§
Source§impl Semigroup for Ordering
This combines two Ordering operations into one. ‘first.cmp(second)’ for example gives back an Ordering.
If we have several types you wish to compare say name and age for example. We can first order by name and then age.
impl Semigroup for Ordering
This combines two Ordering operations into one. ‘first.cmp(second)’ for example gives back an Ordering. If we have several types you wish to compare say name and age for example. We can first order by name and then age.
§Examples
use partial_functional::semigroup::Semigroup;
struct Person {
name: String,
age: u8,
}
let first = Person { name: String::from("Chris"), age: 43 };
let second = Person { name: String::from("Chris"), age: 23 };
let fst_compared_to_snd = first.name.cmp(&second.name).combine(first.age.cmp(&second.age));
assert_eq!(Ordering::Greater, fst_compared_to_snd);
Source§impl<T, E> Semigroup for Result<T, E>
Returns the first Result if it’s an Ok variant, otherwise returns the second
impl<T, E> Semigroup for Result<T, E>
Returns the first Result if it’s an Ok variant, otherwise returns the second
§Examples
use partial_functional::semigroup::Semigroup;
let five: Result<u32, &'static str> = Ok(5);
let two_kb: Result<u32, &'static str> = Ok(2048);
let err: Result<u32, &'static str> = Err("An error occured");
let err_again: Result<u32, &'static str> = Err("Another error");
assert_eq!(Ok(5), five.combine(err.clone()));
assert_eq!(Ok(2048), two_kb.combine(five.clone()));
assert_eq!(Ok(2048), two_kb.combine(five));
assert_eq!(Err("Another error"), err.combine(err_again));
Source§impl<T: Semigroup> Semigroup for Option<T>
Returns the combination of both Some variants, if one is None then the other is returned.
impl<T: Semigroup> Semigroup for Option<T>
Returns the combination of both Some variants, if one is None then the other is returned.
use partial_functional::semigroup::Semigroup;
let five = Some(5);
let ten = Some(10);
assert_eq!(Some(15), five.combine(ten));
assert_eq!(Some(5), five.combine(None));
assert_eq!(Some(10), None.combine(ten));