簡體   English   中英

您可以在 rust (pyo3) 中編寫庫的一部分,但在 python 中編寫 rest 嗎?

[英]Can you write part of a library in rust (pyo3) but the rest in python?

我想在 rust(帶 PYo3)中編寫 python 模塊的一部分(帶有 PYo3),但也部分在 python 中,所以像......

src/utils.rs:

use pyo3::prelude::*;

#[pyfunction]
fn sum_as_string(a: usize, b: usize) -> PyResult<String> {
    Ok((a + b).to_string())
}

#[pymodule]
fn nvm(_py: Python, m: &PyModule) -> PyResult<()> {
    m.add_function(wrap_pyfunction!(sum_as_string, m)?)?;
    Ok(())
}

src/main.py:

from .utils import sum_as_string

...

主要是好奇這是否可以使用 pyo3 或者我是否必須在 rust 中編寫所有內容,因為我已經看到使用 C/Cython 完成類似的操作(示例)。

是的,這是可能的。 Maturin 是您要查看的項目,因為 pyo3 只是與 python 運行時的綁定。 您可以在此處查看有關混合 python/rust 項目的文檔

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM