Skip to content

Instantly share code, notes, and snippets.

@fcofdez
Last active August 29, 2015 14:14
Show Gist options
  • Save fcofdez/c4082fcbd9446bfe07be to your computer and use it in GitHub Desktop.
Save fcofdez/c4082fcbd9446bfe07be to your computer and use it in GitHub Desktop.
Calling Rust code from python
#![crate_type = "dylib"]
#[no_mangle]
pub extern fn fib(n: i32) -> i32 {
if n < 2 {
n
} else {
fib (n-1) + fib(n -2)
}
}
rustc fib.rs
-> libfib.so is generated.
import ctypes
lib_handler = ctypes.CDLL('./libfib.so')
print lib_handler.fib(ctypes.c_int32(12))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment