我正在尝试为我的数学库在 Rust 中实现一个向量类。
#[pyclass]
struct Vec2d {
#[pyo3(get, set)]
x: f64,
#[pyo3(get, set)]
y: f64
}
Run Code Online (Sandbox Code Playgroud)
But I can't figure out how I can overload the standard operators (+, -, *, /)
I Tried implementing the Add trait from std::ops with no luck
impl Add for Vec2d {
type Output = Vec2d;
fn add(self, other: Vec2d) -> Vec2d {
Vec2d{x: self.x + other.x, y: self.y + other.y }
}
}
Run Code Online (Sandbox Code Playgroud)
I also tried adding __add__ method to the #[pymethods] block …