gpt4 book ai didi

rust - 如何使用PyO3在Rust代码中嵌入的Python代码中执行地板分割?

转载 作者:行者123 更新时间:2023-12-03 11:43:43 32 4
gpt4 key购买 nike

我正在尝试在Rust中内联Python代码,但是当Python代码中包含楼层分割//的运算符时,它会失败,就像rust注释一样,该运算符被忽略。

例如:

#![feature(proc_macro_hygiene)]
use inline_python::python;

fn main() {
python! {
print("Hi from PyO3")
foo = 37.46 // 3
print(foo)
}
}

即使它应该打印 37.46(37.46底限除以3的结果)也将打印 12.0

一个可能的解决方案是用 foo = math.floor(37.43 / 3)替换此划分,但我希望不必尽可能地修改Python代码。我还担心它可能会影响性能。

有没有办法在PyO3的Rust代码中嵌入的Python代码中使用地板除法运算符(或同等功能)?

最佳答案

the documentation:

The // and //= operators are unusable, as they start a comment.

Workaround: you can write ## instead, which is automatically converted to //.


所以尝试
#![feature(proc_macro_hygiene)]
use inline_python::python;

fn main() {
python! {
print("Hi from PyO3")
foo = 37.46 ## 3
print(foo)
}
}

关于rust - 如何使用PyO3在Rust代码中嵌入的Python代码中执行地板分割?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59377303/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com