mirror of
https://github.com/ahgamut/rust-ape-example.git
synced 2024-10-31 21:48:49 +00:00
50 lines
1 KiB
Rust
50 lines
1 KiB
Rust
|
// ./src/trait/ops.md
|
||
|
|
||
|
|
||
|
use std::ops;
|
||
|
|
||
|
struct Foo;
|
||
|
struct Bar;
|
||
|
|
||
|
#[derive(Debug)]
|
||
|
struct FooBar;
|
||
|
|
||
|
#[derive(Debug)]
|
||
|
struct BarFoo;
|
||
|
|
||
|
// The `std::ops::Add` trait is used to specify the functionality of `+`.
|
||
|
// Here, we make `Add<Bar>` - the trait for addition with a RHS of type `Bar`.
|
||
|
// The following block implements the operation: Foo + Bar = FooBar
|
||
|
impl ops::Add<Bar> for Foo {
|
||
|
type Output = FooBar;
|
||
|
|
||
|
fn add(self, _rhs: Bar) -> FooBar {
|
||
|
println!("> Foo.add(Bar) was called");
|
||
|
|
||
|
FooBar
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// By reversing the types, we end up implementing non-commutative addition.
|
||
|
// Here, we make `Add<Foo>` - the trait for addition with a RHS of type `Foo`.
|
||
|
// This block implements the operation: Bar + Foo = BarFoo
|
||
|
impl ops::Add<Foo> for Bar {
|
||
|
type Output = BarFoo;
|
||
|
|
||
|
fn add(self, _rhs: Foo) -> BarFoo {
|
||
|
println!("> Bar.add(Foo) was called");
|
||
|
|
||
|
BarFoo
|
||
|
}
|
||
|
}
|
||
|
|
||
|
fn part0() {
|
||
|
println!("Foo + Bar = {:?}", Foo + Bar);
|
||
|
println!("Bar + Foo = {:?}", Bar + Foo);
|
||
|
}
|
||
|
|
||
|
pub fn main() {
|
||
|
part0();
|
||
|
}
|
||
|
|