Last active
August 29, 2015 14:14
-
-
Save fcofdez/c4082fcbd9446bfe07be to your computer and use it in GitHub Desktop.
Calling Rust code from python
This file contains hidden or 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
#![crate_type = "dylib"] | |
#[no_mangle] | |
pub extern fn fib(n: i32) -> i32 { | |
if n < 2 { | |
n | |
} else { | |
fib (n-1) + fib(n -2) | |
} | |
} |
This file contains hidden or 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
rustc fib.rs | |
-> libfib.so is generated. |
This file contains hidden or 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
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