{"slug": "calling-rust-from-python-pypy-using-cffi-c-foreign-function-interface", "title": "Calling Rust from Python/PyPy using CFFI (C Foreign Function Interface)", "summary": "This article demonstrates how to create a library in Rust and call it from Python (both CPython and PyPy) using the C Foreign Function Interface (CFFI), which offers lower call overhead than ctypes and works with both Python implementations. The author recommends installing Rust via multirust for managing different Rust versions and compatibility testing. The demo involves compiling a Rust file (treble.rs) into a shared library (libtreble.dylib) and then calling it from a Python test script.", "body_md": "This is a small demo of how to create a library in Rust and call it from Python (both CPython and PyPy) using the CFFI instead of ctypes\n.\nBased on http://harkablog.com/calling-rust-from-c-and-python.html (dead) which used ctypes\nCFFI is nice because:\n- Reads C declarations (parses headers)\n- Works in both CPython and PyPy (included with PyPy)\n- Lower call overhead than\nctypes\nfrom either of:\nbrew install rust\n- Multirust\nI recommend installing Rust via multirust\nso that you can compile projects that use unstable features (core) as well as testing for Rust 1.0,1.1, etc backwards compatibility.\nThe per-directory override mechanism is especially nice.\nrustc treble.rs\nThis will create a libtreble.dylib\npython test.py\n<cffi.api.FFILibrary_./libtreble.dylib object at 0x1089d5490>\nmath from rust! 30", "url": "https://wpnews.pro/news/calling-rust-from-python-pypy-using-cffi-c-foreign-function-interface", "canonical_source": "https://gist.github.com/seanjensengrey/f5d73bbdf22cfa1ad463", "published_at": "2015-05-27 04:16:46+00:00", "updated_at": "2026-05-22 12:38:35.105983+00:00", "lang": "en", "topics": ["developer-tools", "open-source"], "entities": ["Rust", "Python", "PyPy", "CFFI", "ctypes", "multirust", "CPython"], "alternates": {"html": "https://wpnews.pro/news/calling-rust-from-python-pypy-using-cffi-c-foreign-function-interface", "markdown": "https://wpnews.pro/news/calling-rust-from-python-pypy-using-cffi-c-foreign-function-interface.md", "text": "https://wpnews.pro/news/calling-rust-from-python-pypy-using-cffi-c-foreign-function-interface.txt", "jsonld": "https://wpnews.pro/news/calling-rust-from-python-pypy-using-cffi-c-foreign-function-interface.jsonld"}}