RustPort is an NPM package as well as a command-line tool that automates the creation of TypeScript Foreign Function Interface (FFI) bindings for Rust libraries created by Tahcin Ul Karim (Mycin). This means you can call blazing-fast Rust functions from your JavaScript or TypeScript projects effortlessly.
Perfect for Bun-powered apps, RustPort takes care of all the annoying binding work, so you donโt have to.
Because manually writing FFI bindings is like writing assemblyโpainful and unnecessary. RustPort offers:
โ Zero-Hassle Rust FFI Binding Generation โ Just run a command, and boom, your Rust functions are ready in TypeScript.
โ Flawless Integration with Bun โ If youโre using Bun, RustPort is your new best friend.
โ Effortless Type Mapping โ Converts Rust types into TypeScript like magic.
โ Super Simple CLI โ Generate and clean bindings in seconds with one command.
RustPort can be installed globally via your favorite package manager:
# Using npm
npm install -g rustport
# Using yarn
yarn global add rustport
# Using bun
bun add -g rustportInside your project, create a lib/ directory where RustPort will generate its magic:
mkdir -p lib/rsCreate a Rust file, say hello.rs, inside lib/rs/:
#[no_mangle]
pub extern "C" fn say_hello() {
println!("Hello, Rustacean! ๐ฆ");
}Yes, thatโs all you need. No weird macros, no painful setup.
Now, let RustPort do its thing:
rustport generate lib/This will:
โ
Compile your Rust functions into dynamic libraries (.so / .dll / .dylib).
โ
Generate TypeScript FFI bindings.
โ
Create an index.ts file inside lib/.
Now, call Rust functions as if they were just another JavaScript function:
import { sayHello } from "./lib";
sayHello(); // Prints: Hello, Rustacean! ๐ฆNote that for now it only supports Bun, but weโre working on supporting Deno and Node.js. For this, you'll need Bun installed on your machine.
Install Bun using npm, yarn, or bun:
npm install -g bunNow run the file.
bun run fileName.tsThatโs it! You just called Rust from TypeScript without touching a single binding manually. ๐
Generates Rust FFI bindings and compiles Rust files to dynamic libraries.
rustport generate lib/Wipes out all generated files and libraries, leaving no trace of your sins. ๐
rustport cleanDisplays the help menu.
rustport helpWant to rename functions or create wrappers? Just tweak the generated index.ts:
import { sayHello as rustSayHello } from "./lib";
export function sayHello() {
rustSayHello();
}Rust loves numbers. So do we. Let's make them talk:
#[no_mangle]
pub extern "C" fn add_numbers(a: i32, b: i32) -> i32 {
a + b
}Generate bindings:
rustport generate lib/import { addNumbers } from "./lib";
console.log("5 + 3 =", addNumbers(5, 3));Rust can return strings too, but remember: memory management is a thing!
use std::ffi::{CString, CStr};
use std::os::raw::c_char;
#[no_mangle]
pub extern "C" fn greet(name: *const c_char) -> *mut c_char {
let c_str = unsafe { CStr::from_ptr(name) };
let r_str = format!("Hello, {}!", c_str.to_str().unwrap());
CString::new(r_str).unwrap().into_raw()
}Generate bindings:
rustport generate lib/import { greet } from "./lib";
console.log(greet("Alice")); // Prints: Hello, Alice!Yes, Rust just greeted Alice from TypeScript. Wild, huh? ๐ฒ
1. RustPort fails to generate bindings
- Make sure Rust and Cargo are installed.
- Check if
lib/rs/contains valid Rust files. - Run
cargo buildinsidelib/rs/to debug compilation issues.
2. TypeScript FFI calls fail
- Ensure that Rust functions are exported with
#[no_mangle]. - Verify the correct function signatures in TypeScript.
3. Dynamic libraries not loading?
- On Windows, ensure
.dllfiles are in the correct directory. - On macOS, use
install_name_toolto set the correct paths. - On Linux, check
LD_LIBRARY_PATH.
Hereโs what a simple RustPort-powered Bun project might look like:
my-project/
โโโ lib/
โ โโโ rs/
โ โ โโโ hello.rs
โ โ โโโ math.rs
โ โ โโโ string_utils.rs
โ โ โโโ Cargo.toml
โโโ src/
โ โโโ main.ts
โโโ package.json
import { sayHello, addNumbers, greet } from "../lib";
sayHello();
console.log(addNumbers(10, 20));
console.log(greet("Bob"));If you love RustPort and want to make it even better, open an issue or send a PR on GitHub! If this tool saved you time, consider buying us a virtual coffee. โ
RustPort is licensed under MIT. See the LICENSE file for details.
Now go forth and build fast, Rust-powered Bun projects! ๐
Note: Also check out ZigPort for a similar tool that generates TypeScript FFI bindings for Zig libraries.