A rudimentary example of calling rust from cython
I've seen a number of examples of calling Rust from python, including Dan Callahad's talk at PyCon, this example from J. Clifford Dyer as well as in the official Rust book. All of the examples have used either the ctypes built-in module or CFFI.
Here is a simple example of calling a function defined in rust that doubles a double
from within Cython.
The example basically relies on the formalism that one would use to call Rust from C. This involves writing a .h
header file
and linking to the dynamic library generated by rust via the setup.py
file.
Basic instructions for running the example:
cargo build --release
python setup.py build_ext --inplace
Then launching IPython:
In [1]: import cytest
In [2]: cytest.call_rust_double(10.0)
Out[2]: 20.0
This example is obviously not very useful, but I wanted to demonstrate the capability.