को देखते हुए निम्नलिखित struct:विभिन्न आरएचएस प्रकारों और मूल्यों को वापस करने के लिए ऑपरेटर को ओवरलोड किया जा सकता है?
struct Vector3D {
x: f32,
y: f32,
z: f32
}
मैं अपने *
ऑपरेटर को ओवरलोड एक डॉट उत्पाद करने के लिए जब दाहिने हाथ की ओर एक Vector3D
है, और जब आरएचएस एक f32
है एक तत्व के लिहाज से गुणा करने के लिए करना चाहते हैं। मेरे कोड इस तरह दिखता है:
// Multiplication with scalar
impl Mul<f32, Vector3D> for Vector3D {
fn mul(&self, f: &f32) -> Vector3D {
Vector3D {x: self.x * *f, y: self.y * *f, z: self.z * *f}
}
}
// Multiplication with vector, aka dot product
impl Mul<Vector3D, f32> for Vector3D {
fn mul(&self, other: &Vector3D) -> f32 {
self.x * other.x + self.y * other.y + self.z * other.z
}
}
संकलक पहले impl ब्लॉक के लिए कहते हैं: अन्य कार्यान्वयन के लिए
Vector3D.rs:40:1: 44:2 error: conflicting implementations for trait `std::ops::Mul`
Vector3D.rs:40 impl Mul<f32, Vector3D> for Vector3D {
...
Vector3D.rs:53:1: 57:2 note: note conflicting implementation here
Vector3D.rs:53 impl Mul<Vector3D, f32> for Vector3D {
...
और इसके विपरीत।
यह http://stackoverflow.com/questions/24554738/rust-resolving-trait-impl-conflicts का डुप्लिकेट है। –
@ChrisMorgan यह वास्तव में एक डुप्लिकेट नहीं है। इस प्रश्न में दो ठोस 'इम्प्ल' हैं, जबकि उस प्रश्न में एक ठोस 'इम्प्ल' है जो ठोस के साथ है। – huon