I have been following a Rust tutorial, that uses a custom type. So a library was defined, that was used in a rust binary later via a cargo.toml dependency. I ended up with a couple of issues:
- E0368: A binary assignment operator like += or ^= was applied to a type that doesn't support it.
- E0600: cannot apply unary operator '-'.
Does Rust:
- necessitate Traits that provide these capability to be defined back in library code (money_typesafe below) referenced by the cargo.toml dependency (of main.rs) that gets used in the binary.
- allow Traits to be applied in the using binary.
- allow both 1) & 2)?
If 2, is permitted, does the syntax change on defining the Trait in any way?
cargo.toml
...
[dependencies]
money_typesafe = {path = "../../../2-Traits/10-Day-2-Assignment/day2assign/"}`
main.rs
...
use money_typesafe::currencies::{Money,GBP};
i.e. For 2) Can I add traits to Money or GBP in main.rs?
Footnote:
I did find the code for the tutorial on github. It followed the option 1 scenario. Not sure if other option exists extending capability of type in another place.
It involved:
- AddAssign
- Neg (I think the example give here isn't helpful. Not doing something with a numeric type and using an enum just muddies the waters).
use std::ops::AddAssign;
use std::ops::Neg;