小编N0n*_*ame的帖子

如何在 PyO3 中实现 python 运算符

我正在尝试为我的数学库在 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 …

python rust pyo3

4
推荐指数
1
解决办法
499
查看次数

标签 统计

pyo3 ×1

python ×1

rust ×1