Skip to content

Instantly share code, notes, and snippets.

@jcbritobr
Created October 18, 2020 14:02
Show Gist options
  • Save jcbritobr/7afafea5cc1b7f9797fe95bb2469dc68 to your computer and use it in GitHub Desktop.
Save jcbritobr/7afafea5cc1b7f9797fe95bb2469dc68 to your computer and use it in GitHub Desktop.
Rust python module with pyo3
use pyo3::{
prelude::pyfunction, prelude::pymodule, types::PyModule, wrap_pyfunction, PyResult, Python,
};
#[pyfunction]
fn sum_numbers(a: usize, b: usize) -> PyResult<usize> {
Ok(a + b)
}
#[pymodule]
fn py_math(_py: Python, m: &PyModule) -> PyResult<()> {
m.add_function(wrap_pyfunction!(sum_numbers, m)?)?;
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_sum_numbers() {
assert_eq!(5, sum_numbers(2, 3).expect("cant sum"));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment