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:

File src/lib.rs:

The next step is to build the library:

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

Python

Invoking the add function from Python is very easy:

Ruby

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

Ruby code:

Zig

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:

Generate header file from the library.

Create Zig application in zig2rust:

Compile Zig application:

Run the application: