cd /news/developer-tools/calling-rust-from-python-pypy-using-… · home topics developer-tools article
[ARTICLE · art-8802] src=gist.github.com ↗ pub= topic=developer-tools verified=true sentiment=· neutral

Calling Rust from Python/PyPy using CFFI (C Foreign Function Interface)

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.

read1 min views17 publishedMay 27, 2015

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 . Based on http://harkablog.com/calling-rust-from-c-and-python.html (dead) which used ctypes CFFI is nice because:

- Reads C declarations (parses headers)
- Works in both CPython and PyPy (included with PyPy)
  • Lower call overhead than ctypes from either of: brew install rust
  • Multirust I recommend installing Rust via multirust so that you can compile projects that use unstable features (core) as well as testing for Rust 1.0,1.1, etc backwards compatibility. The per-directory override mechanism is especially nice. rustc treble.rs This will create a libtreble.dylib python test.py <cffi.api.FFILibrary_./libtreble.dylib object at 0x1089d5490> math from rust! 30
── more in #developer-tools 4 stories · sorted by recency
── more on @rust 3 stories trending now
sponsored brought to you by zahid.host 4,200+ EU-deployed projects
reading about agents? ship yours in a single git push.

Run your AI side-project on zahid.host

EU-based hosting, git-push deploys, automatic HTTPS, no cold starts. Free tier with a custom domain — perfect for shipping the agent you just read about.

$git push zahid main
Live at https://your-agent.zahid.host
Get free account → Pricing
from €0/mo · no card required
LIVE [news/calling-rust-from-py…] indexed:0 read:1min 2015-05-27 ·