PyO3 を使用して、Rust から Python&str
に型を渡すことができます。String
#[pyfunction]
fn test_str(py: Python) -> &str {
"this is a &str"
}
#[pyfunction]
fn test_string(py: Python) -> String {
"this is a String".to_string()
}
そしてPythonはこれらをうまく呼び出すことができます:
>>> test_str(), type(test_str())
('this is a &str', <class 'str'>)
>>> test_string(), type(test_string())
('this is a String', <class 'str'>)
PyResult<&str>
これらをPyResult<String>
同じ動作でラップすることもできます。
ここでメモリが適切に処理されるようにするには、何を知る必要があり、他の手順を実行する必要がありますか? String
同じ文字列への参照を維持しようとしていない場合、必要に応じてそれらを解放できるように、s について GIL に伝える必要がありますか?
さらに行う必要がある場合、 Rust を作成するときなど、他のメソッドでも同じことを行う必要がありますstruct
か?
#[pyfunction]
fn new_thing(py: Python) -> Thing {
Thing { foo: 1, bar: true }
}