Skip to main content

No project description provided

Project description

PyO2: A lightweight method to call Rust code from Python

Usage

File: Cargo.toml

[package]
name = "mylib"
version = "0.1.0"
edition = "2021"

[lib]
crate-type = ["cdylib"]

[dependencies]
pyo2 = "0.1.0"

File: src/lib.rs

use pyo2::{PyStr, PyVec};

#[no_mangle]
pub extern "C" fn test(name: &PyStr, numbers: &mut PyVec<i64>) {
    println!("Hello, {}!", unsafe { name.as_str_unchecked() });
    println!("Sum of numbers: {}", numbers.iter().cloned().sum::<i64>());
    numbers[0] = 6;
}```

File: *test.py*

```python
from pyo2 import RustDLL

dll = RustDLL('./libmylib.so')

s = 'World'
lst = [1, 2, 3, 4, 5]
dll.test(s, lst)
print(lst)

Output:

Hello, World!
Sum of numbers: 15
[6, 2, 3, 4, 5]

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pyo2-0.1.0.tar.gz (2.3 kB view hashes)

Uploaded Source

Built Distribution

pyo2-0.1.0-py3-none-any.whl (2.6 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page