差分
このページの2つのバージョン間の差分を表示します。
| 両方とも前のリビジョン前のリビジョン次のリビジョン | 前のリビジョン | ||
| youtube:python-mixing-007 [2024/02/28 18:26] – 削除 - 外部編集 (不明な日付) 127.0.0.1 | youtube:python-mixing-007 [2024/07/12 02:51] (現在) – freemikan | ||
|---|---|---|---|
| 行 1: | 行 1: | ||
| + | ====== RustとPythonの様子見 ====== | ||
| + | |||
| + | 作成日: 2023-08-12 (土) | ||
| + | |||
| + | [[https:// | ||
| + | |||
| + | プロジェクトレイアウト | ||
| + | . | ||
| + | └── example/ | ||
| + | ├── Cargo.toml | ||
| + | └── src/ | ||
| + | └── lib.rs | ||
| + | |||
| + | ===== example ===== | ||
| + | |||
| + | rust-cpythonを使ったPythonの拡張モジュール。 | ||
| + | |||
| + | ==== Cargo.toml ==== | ||
| + | |||
| + | <file toml> | ||
| + | [package] | ||
| + | name = " | ||
| + | version = " | ||
| + | edition = " | ||
| + | |||
| + | # See more keys and their definitions at https:// | ||
| + | [dependencies] | ||
| + | |||
| + | [lib] | ||
| + | name = " | ||
| + | crate-type = [" | ||
| + | |||
| + | [dependencies.cpython] | ||
| + | version = " | ||
| + | features = [" | ||
| + | </ | ||
| + | |||
| + | ==== src/lib.rs ==== | ||
| + | |||
| + | <file rust> | ||
| + | extern crate cpython; | ||
| + | |||
| + | use cpython:: | ||
| + | |||
| + | py_module_initializer!(foo, | ||
| + | m.add(py, " | ||
| + | m.add(py, " | ||
| + | m.add(py, " | ||
| + | Ok(()) | ||
| + | }); | ||
| + | |||
| + | fn greet(name: &str) { | ||
| + | println!(" | ||
| + | } | ||
| + | |||
| + | fn greet_py(_py: | ||
| + | greet(name); | ||
| + | Ok(PyNone) | ||
| + | } | ||
| + | |||
| + | fn add(_py: Python, a: i32, b: i32) -> PyResult< | ||
| + | let out = a + b; | ||
| + | Ok(out) | ||
| + | } | ||
| + | </ | ||
