Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

從 Python 使用 Rust

本章專門說明如何將 Rust 程式碼封裝成 Python 物件。

PyO3 使用 Rust 的「程序巨集」提供強大而簡單的 API,用來標示哪些 Rust 程式碼要對應為 Python 物件。

PyO3 可建立三種 Python 物件:

  • Python 模組,透過 #[pymodule] 巨集
  • Python 函式,透過 #[pyfunction] 巨集
  • Python 類別,透過 #[pyclass] 巨集(另用 #[pymethods] 為這些類別定義方法)

The following subchapters go through each of these in turn.