Rust as Universal Glue: Why Integration with Other Languages is a Superpower
\n\nHave 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\nOn 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\nWhy Integrate Rust with Other Languages?
\n\nImagine: 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\nMain 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
Integrating Rust with Python via PyO3
\n\nPyO3 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\nInstallation and Setup
\nFirst, install maturin — a tool for building Rust Python extensions:
pip install maturin\nCreate a new project:
\nmaturin init --bindings pyo3\nThis creates a structure with Cargo.toml and lib.rs files.
Example: Speeding Up Computations
\nLet'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:
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}\nBuild the module:
\nmaturin develop\nNow in Python:
\nimport rust_fib\nprint(rust_fib.fibonacci(40)) # 102334155\nCompare 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\nPassing Complex Data
\nPyO3 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}\nIn Python:
\nprint(rust_fib.sum_list([1, 2, 3, 4])) # 10\nThis opens up huge possibilities for integrating Rust with Python in data science and web development.
\n\nRust and C: Classic FFI
\n\nRust was created as a replacement for C/C++, so Rust FFI is its native style.