hxn

RUST → HAXE

Native Rust bindings
for Haxe

Write your library once in Rust. hxn generates the Haxe classes and native glue for HashLink, Ash and hxcpp — natively and on wasm.

Install the CLI

Latest release
curl -fsSL https://raw.githubusercontent.com/hx-n/hxn/main/install.sh | bash

macOS & Linux · Windows via Git Bash

Release downloads

QUICK START

Your first native library.

A small Rust library, used from Haxe. Start with Rust, Haxe, and HashLink or hxcpp from git installed.

01 Create a library

hxn new creates a Rust crate and a Haxe program, ready to run.

TerminalBash
hxn new greeter
cd greeter

02 Meet the two sides

These are the files the command creates. Mark the Rust interface and methods; hxn generates the Haxe class and native glue.

src/lib.rsRust
/// Greets people, and counts them.
#[hxn::interface]
pub struct Greeter {
    greeting: String,
    greeted: i32,
}

#[hxn::methods]
impl Greeter {
    pub fn new(greeting: &str) -> Self {
        Self {
            greeting: greeting.to_owned(),
            greeted: 0,
        }
    }

    /// A greeting for `name`. Fails for an empty name.
    pub fn greet(&mut self, name: &str) -> Result<String, &'static str> {
        if name.is_empty() {
            return Err("nobody to greet");
        }
        self.greeted += 1;
        Ok(format!("{}, {name}!", self.greeting))
    }

    /// How many have been greeted.
    pub fn greeted(&self) -> i32 {
        self.greeted
    }
}
haxe/Main.hxHaxe
class Main {
	static function main() {
		var greeter = new Greeter("Hello");
		Sys.println(greeter.greet("Haxe"));
		Sys.println(greeter.greet("Rust"));
		Sys.println('${greeter.greeted()} greeted');
		try {
			greeter.greet("");
		} catch (error:haxe.Exception) {
			Sys.println('refused: ${error.message}');
		}
		greeter.close();
	}
}

03 Run it

hxn builds the binding, compiles your Haxe program, and runs it on the target you choose. Both produce the same output.

TerminalBash
hxn run hl
# Or use hxcpp:
hxn run cpp
OutputEither runtime
Hello, Haxe!
Hello, Rust!
2 greeted
refused: nobody to greet
Explore more examples

TUTORIALS

Build with hxn.

Small, runnable examples for the CLI and the APIs you use from Haxe.

All tutorials