Created
October 18, 2020 14:02
-
-
Save jcbritobr/7afafea5cc1b7f9797fe95bb2469dc68 to your computer and use it in GitHub Desktop.
Rust python module with pyo3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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