Rust as Glue: Integration with Python, C and Other Languages

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Rust as Universal Glue: Why Integration with Other Languages is a Superpower

\n\n

Have you ever dreamed of writing a high-performance module in Rust, but didn't want to rewrite the entire project in Python? Or maybe you need to add safe memory management to an old C library? Welcome to the world of integrating Rust with other languages. Here Rust acts not just as a standalone language, but as a powerful "glue" that connects ecosystems.

\n\n

On pythonlib.ru we often talk about Python, but today we'll make a sharp turn. Rust is not only about speed and safety, but also incredible flexibility. It can integrate with Python via PyO3, call C functions through FFI, and even compile to WebAssembly for the browser. In this article, we'll break down three main scenarios, show real code, and give practical tips. Let's go!

\n\n

Why Integrate Rust with Other Languages?

\n\n

Imagine: you have a huge Python project that slows down due to loops. You can rewrite the critical section in Rust, get a 10-100x speed boost, and not touch the rest of the code. Or you are working with a legacy C library and want to wrap it in a safe Rust interface. Rust is ideal for this thanks to Foreign Function Interface (FFI).

\n\n

Main reasons for integration:

\n
    \n
  • Performance — Rust is faster than Python and JavaScript in computational tasks.
  • \n
  • Safety — Rust protects against memory leaks and data races, which is critical for C/C++.
  • \n
  • Ecosystem — you get access to Rust libraries without leaving your native language.
  • \n
  • WebAssembly — Rust code runs in the browser at near-native speed.
  • \n
\n\n

Integrating Rust with Python via PyO3

\n\n

PyO3 is the most popular way to bind Rust and Python. You write functions in Rust, compile them into a native module (.so or .pyd), and import them like a regular Python module. Sounds magical? In practice, it's simple.

\n\n

Installation and Setup

\n

First, install maturin — a tool for building Rust Python extensions:

\n
pip install maturin
\n

Create a new project:

\n
maturin init --bindings pyo3
\n

This creates a structure with Cargo.toml and lib.rs files.

\n\n

Example: Speeding Up Computations

\n

Let's say we need to quickly calculate Fibonacci numbers. In Python this can be slow, but in Rust it's lightning fast. Here's the code in src/lib.rs:

\n
use pyo3::prelude::*;\n\n#[pyfunction]\nfn fibonacci(n: u32) -> u64 {\n    match n {\n        0 => 0,\n        1 => 1,\n        _ => fibonacci(n - 1) + fibonacci(n - 2),\n    }\n}\n\n#[pymodule]\nfn rust_fib(_py: Python, m: &PyModule) -> PyResult<()> {\n    m.add_function(wrap_pyfunction!(fibonacci, m)?)?;\n    Ok(())\n}
\n

Build the module:

\n
maturin develop
\n

Now in Python:

\n
import rust_fib\nprint(rust_fib.fibonacci(40))  # 102334155
\n

Compare the speed with pure Python — the difference will be tens of times! This is a perfect example of integrating Rust with Python for optimization.

\n\n

Passing Complex Data

\n

PyO3 supports not only numbers, but also lists, dictionaries, and even numpy arrays. For example, you can pass a list and process it in Rust:

\n
#[pyfunction]\nfn sum_list(numbers: Vec<i32>) -> i32 {\n    numbers.iter().sum()\n}
\n

In Python:

\n
print(rust_fib.sum_list([1, 2, 3, 4]))  # 10
\n

This opens up huge possibilities for integrating Rust with Python in data science and web development.

\n\n

Rust and C: Classic FFI

\n\n

Rust was created as a replacement for C/C++, so Rust FFI is its native style.

Blogs

Book Recommendations