In Rust, I want to add the PartialEq trait to struct that's defined in an external crate. The PartialEq trait cannot simply be added to the external struct since I want to compare it to a custom type. How can I do that?
I know you can define your own trait (e.g., DoSomething
in the example below), implement that for the external struct, then import that trait to use it. However, I can't figure out how to do something similar with any of the standard traits like PartialEq.
// a.rs
use external::Struct;
pub trait DoSomething {
fn can_do_something(&self) -> bool;
}
impl DoSomething for Struct {
pub fn can_do_something(&self) -> bool {
true
}
}
// b.rs
use external::Struct;
use a::DoSomething;
fn main() {
let struct = Struct::new();
println!("can it do something? {}", struct.can_do_something());
}
When I try to do something similar for PartialEq, I get the following error:
use external::Struct;
impl PartialEq for Struct {
^^^^^^^^^ only traits defined in the current crate can be implemented
for types defined outside of the crate define and implement
a trait or new type instead
// ...
}