2 minutes
How to Create Rust Python Bindings
Rust is super fast. Python is super flexible. Porting slow python code to rust can make your life a lot easier, and it’s not too difficult to set up.
I will demonstrate rust bindings for summing the integers in a large text file containing a billion digits that looks like
6,9,8,3,0,1,8,4,9,7,6,3,4,2,6,0,0,5,1,1, . . . ,4,5,9,3,3,2,8,3
General steps
Install Rust and Maturin
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
pip install maturin
See also install rust and install maturin
Set up boilerplate
# select "pyo3" here
maturin new cool_rust_bindings
cd cool_rust_bindings
Add your function
Open src/lib.rs
and change it to the following:
use pyo3::prelude::*;
use std::fs::File;
use std::io::{self, Read};
#[pyfunction]
fn sum_file(filename:&str) -> io::Result<i64> {
let mut file = File::open(filename)?;
let mut contents = String::new();
file.read_to_string(&mut contents)?;
let mut total:i64 = 0;
for x in contents.chars() {
if x != ','{
total += (x as i64) - 48;
}
}
Ok(total)
}
#[pymodule]
fn cool_rust_bindings(_py: Python, m: &PyModule) -> PyResult<()> {
m.add_function(wrap_pyfunction!(sum_file, m)?)?;
Ok(())
}
Here we’re just creating a simple function that sums the integers in a file. The rust code gave a 3x speedup over the corresponding python code. Depending on the task, I’ve gotten 10-20x speedups.
Compile and import
Run the following from the cool_rust_bindings
directory
maturin develop
This will add cool_rust_bindings
to your python path. Use it like
import cool_rust_bindings
cool_rust_bindings.sum_file('large_file.csv')
Read other posts