How to call Rust functions from Python, Ruby and Zig

Rust language is great for creating libraries and drivers which can be consumed by other languages like Python, Ruby or Zig.

Let’s consider following Rust application Cargo.toml:

name = "rustlib"
version = "0.1.0"
edition = "2021"

name = "rust_lib"
crate-type = ["dylib"]

File src/

pub extern "C" fn add(left: usize, right: usize) -> usize {
    left + right

The next step is to build the library:

cargo build --release

The output library is stored in ./target/release/


Invoking the add function from Python is very easy:

import ctypes
lib = ctypes.CDLL("./target/release/")


In case of Ruby we will need ffi gem.
Installation on OpenSuse:

sudo zypper install ruby3.1-rubygem-ffi

Ruby code:

require 'ffi'

module MyLib
  extend FFI::Library
  ffi_lib './target/release/'
  attach_function :add, [ :int, :int ], :int

puts MyLib.add 1, 2


Zig will require little bit more stuff. We need to generate C headers from Rust, which then can be loaded to Zig. Install cbindgen for the conversion:

cargo install cbindgen

Generate header file from the library.

cbindgen --lang c --output rustlib.h

Create Zig application in zig2rust:

const std = @import("std");
const rustlib = @cImport(@cInclude("rustlib.h"));

pub fn main() !void {
    const stdout =;
    const result = rustlib.add(1,2);
    try stdout.print("Result is {d}.\n", .{result});

Compile Zig application:

zig build-exe zig2rust.zig -I. -l target/release/

Run the application:

No votes yet.
Please wait...

9. February 2023 at 14:26 - Development (Tags: , , , , , ). Both comments and pings are currently closed.

Comments are closed.