Skip to content

Instantly share code, notes, and snippets.

@chhetripradeep
Created May 1, 2019 17:02
Show Gist options
  • Save chhetripradeep/3c64187f83f598551b0bb05649553b91 to your computer and use it in GitHub Desktop.
Save chhetripradeep/3c64187f83f598551b0bb05649553b91 to your computer and use it in GitHub Desktop.
Rust Python Bindings
#[macro_use] extern crate cpython;
use cpython::{PyResult, Python};
py_module_initializer!(libmyrustlib, initlibmyrustlib, PyInit_libmyrustlib, |py, m| {
m.add(py, "__doc__", "This module is implemented in Rust.")?;
m.add(py, "rust_concat", py_fn!(py, rust_concat(val: i64)))?;
Ok(())
});
fn rust_concat(_: Python, val: i64) -> PyResult<String> {
let mut result = String::new();
for i in 0..val {
result.push_str(&i.to_string());
}
Ok(result)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment