integration between Python and Rust - Part 2

In this post we are going to write a new module for python: a very simple function exported from Rust that we can consume in the Python interpreter. We’ll leverage the PyO3 Rust bindings for the Python interpreter. Let’s start with a new Cargo project: $ cargo init --lib demo_rust_lib and insert the required settings in Cargo.toml: [package] name = "rusty" version = "0.1.0" edition = "2021" [lib] name="rusty" crate-type = ["cdylib"] [dependencies.pyo3] version = "*" [features] extension-module = ["pyo3/extension-module"] default = ["extension-module"] now it’s a matter to write our library; luckily the PyO3 library exposes a lot of useful types for python interop; the only thing we need to add is an extra fn named as our module that “exports” the functions we want to make available in the Python layer: ...

January 7, 2022 · Andrea Manzini

integration between Python and Rust - Part 1

Let’s get our feet wet; in this first part I’ll write about a very simple way to interface Rust and Python. First of all let’s build a Rust dynamic library with some basic functions. // this file is: src/lib.rs #[no_mangle] pub extern "C" fn hello() { println!("Hello from the library!"); } #[no_mangle] pub extern "C" fn sum(a: i32, b: i32) -> i32 { a + b } your Cargo.toml should look like this: [package] name = "pyrust" version = "0.1.0" edition = "2018" [dependencies] [lib] crate-type = ["cdylib"] compile the library with cargo build ...

August 18, 2021 · Andrea Manzini