reflaxe.rust compiles Haxe 4.3.7 code to Rust. It is a compiler target built on
Reflaxe.
- What it produces: an ordinary Cargo project with Rust source that you can read. Cargo then builds a native binary from it.
- Who it is for: Haxe teams that want native Rust binaries and Rust crates, and Rust-aware teams that want to write typed Haxe against Rust-first APIs.
- What it aims for: production-ready, near-instant compilation along two paths: ordinary Haxe that
becomes safe Rust the compiler optimizes automatically, and
metal, which lets you write Rust itself through Haxe and runs like handwritten Rust. - How mature it is: pre-1.0. It is usable in production only on the paths that your own application tests. See Maturity.
- Fastest first run: three commands, in Quick start.
This README describes the main branch. A published release can be older; the
releases page lists what each one contains.
Compile coverage is broad, but runtime proof varies by operation: the
semantic confidence summary lists which portable operations
executable tests prove. Use reflaxe.rust in production only on paths that your own application tests,
and add smoke tests for networking, TLS, databases, processes, and threads. Before relying on it, read
the Production Readiness guide, the
feature support matrix, and the release policy in
SemVer And Release Posture.
Architecture-claim status: 3 closed, 7 qualified, 1 open. The implemented architecture disproves the old claim that a usable Haxe-to-Rust backend is impossible without either a tracing GC or lifetime annotations everywhere, but the evidence still supports bounded, qualified claims rather than universal parity. Review the exact evidence, exclusions, and blocking Beads in the generated architecture capability claims; this summary does not authorize stable 1.0 or an unqualified “all Haxe to handwritten-quality Rust” claim.
You need Node.js >=22.14 <23 with npm, Rust 1.96.0 or newer with Cargo, and Git. npm install
installs the pinned Haxe toolchain through lix. See the
Rust toolchain policy.
From a checkout of this repository:
npm install
cd examples/hello
npx haxe compile.hxml && (cd out && cargo run -q)The program prints hi. The generated Cargo project is in examples/hello/out.
Next steps:
- Plain-language tour: Start Here guide.
- Common first questions (garbage collection, memory, runtime cost, interop): FAQ.
- Use the compiler in an existing app: Install via lix.
- Check your checkout:
npm testruns the snapshot tests.npm run test:allruns the full local harness, which takes much longer. GitHub Actions runs the same harness split into parallel jobs.
npm run dev:new-project -- ./my_haxe_rust_app
cd my_haxe_rust_app
cargo hx runThe command copies the starter template (templates/basic). The new project contains:
cargo hx run,cargo hx test, andcargo hx build --release: one compile and Cargo cycle.cargo hx dev: compile and run again on each change (see Development loop).bash scripts/dev/check-guards.sh: checks for local paths and security wiring, and runs a gitleaks scan when gitleaks is installed.bash scripts/install-git-hooks.sh: installs the pre-commit checks.
Notes:
cargo hxcallshaxefrom yourPATH. SetHAXE_BINor pass--haxe-binto use a different one.- A project created inside this checkout uses the checkout's Haxe setup. For a project anywhere else,
first follow the setup section of the generated
README.mdor Install via lix. cargo hx testfinds methods marked@:rustTestbelow the main class source root. The app does not need a fake call to keep a test class reachable, and ordinary builds leave these test modules out. See Haxe-authored Rust tests.- For an application, commit the generated
Cargo.lockand pass-D rust_cargo_lockedin CI and release builds. Generated manifests use Cargo resolver 3, which prefers dependencies that work with the declared Rust version.
The full generated-app loop is in Workflow.
- Two profiles.
portable(the default) keeps Haxe behavior.metaladds Rust-first APIs and stricter rules for performance-critical code. See Pick your profile. - A runtime only where Haxe needs one. The compiler decides as much as it can at compile time.
The runtime crate
hxrtcovers what Rust has no direct equivalent for:Dynamic, reflection, exceptions, object identity, anonymous records,null, and platform APIs. Inmetal,-D rust_no_hxrtbuilds without it and fails if the output still needs it. See the FAQ. - Only the code you use. The crate contains the classes your program uses from every class path
and pure-Haxe
-lib, as on other Haxe targets. Unused parts of the standard library are left out: hello world emits two Rust modules instead of about thirty. See the FAQ. - Plain Rust structs for small values. A
final classmarked@:rustStructbecomes a RustCopystruct with no allocation and no lock. Inmetalit may also havevarfields and&mut selfmethods, updated in place. This is experimental and has strict rules. An ordinary class with onlyfinalnumber orBoolfields gets the same struct without the metadata when no code compares, nulls, or reflects its values. See Value structs. - Plain
Vecfor local arrays. A localArray<T>that never gets a second name is stored as a RustVec<T>. Every other array stays a shared Haxe array. See Array semantics. - Haxe arithmetic in every build.
Intwraps on overflow in debug builds too, as on other Haxe targets. Operators and conversions onDynamicnumbers follow Haxe rules. See Operators. - Native Rust types where the meaning matches. For example, the admitted
OptionandResultsurfaces ofreflaxe.stdcompile to RustOptionandResult. That shared package is not bundled with this library yet. See Portable near-native guidance. - Typed access to Rust. Bind Rust code with
externclasses, add crates with@:rustCargo, and use the typedrust.*modules for files, processes, TCP, and UDP inmetal. See the Interop guide and the Metal systems facades roadmap. - A map from Rust back to Haxe. Every generated crate includes
rust-source-map.json, which ties generated Rust bytes to Haxe source positions. Rust compiler errors are not yet rewritten into Haxe errors. See Rust-to-Haxe source maps.
Per-feature behavior (strings, iterators, generics, anonymous records, reflection, threads, async) is in the Documentation Index and the feature support matrix.
Speed depends on which Haxe features a program uses.
- Typed arithmetic,
@:rustStructvalues, and local arrays stored asVeccompile to plain Rust. - An ordinary class instance is a shared, locked reference with its own heap allocation, because Haxe
classes have reference semantics. A method that never lets
thisescape works through the reference it was called with, and number orBoolfield reads ofthisin one expression with no call between them share one lock. A shared array takes a lock on each element access.Dynamic, reflection, and exceptions also go through the runtime. - A portable
Stringis shared, immutable text: copying one only counts a new reference. Building new text allocates.
Current measurements, as a ratio of generated-program time to handwritten Rust time:
| Workload | Result | Where it was measured |
|---|---|---|
| Integer hot loop | about 1.0x | committed baseline |
haxe.io.Bytes reads and writes |
about 1.0x | committed baseline |
haxe.Json parse and stringify |
about 1.5x serde_json |
committed baseline |
| JSON parse plus typed validation | about 3.1x to 3.3x | committed baseline |
haxe.Int64 arithmetic |
about 1.0x native i64 (221x before Int64 became a native i64) |
one developer machine; the CI lane is warning-only |
Particles with a @:rustStruct vector in a local array |
about 1.0x to 1.6x idiomatic Rust across runs (24x before the Vec change) |
one developer machine |
Particles updated in place through mutable @:rustStruct methods (metal) |
about 1.1x idiomatic Rust | one developer machine |
| Particles with an ordinary class vector | about 2.2x to 2.5x a handwritten Rc<RefCell<..>> model |
one developer machine |
Strings: record copies, map keys, concatenation, StringBuf, split and join, thread readers |
about 2.8x idiomatic Rust (4.8x before strings shared their text) | one developer machine |
Limits of these numbers:
- They are small benchmarks. There is no measurement of a large application.
- Each timing covers the whole process, including startup and output. In a run of a few milliseconds, startup hides part of the difference in the loop.
- The particles and strings results are report-only: no CI gate checks them. The latest runs are in HXRT overhead benchmarks and, for mutable structs, in Value structs.
- Hot-loop ratios from before the benchmark was repaired are not valid. Read the performance evidence limit first.
Compile time: generating Rust for a large program (about 20,000 generated lines) takes about 11
seconds of CPU time on one machine, down from several minutes. Haxe's own type checking is under a
second of that. Hello world generates in under two seconds. npm run perf:compile-time measures
this. Protocol, cases, and gates: HXRT overhead benchmarks and the
Consumer runtime benchmark corpus.
Select a profile with -D reflaxe_rust_profile=portable|metal.
| Profile | Best for | What you get |
|---|---|---|
portable (default) |
Haxe-first teams | Haxe behavior and code that stays portable to other Haxe targets |
metal |
Rust-aware teams and performance-critical paths | Rust-first APIs (rust.*), stricter boundary rules, and the option to build without the runtime |
Rules of thumb:
- Start in
portable. Addmetalonly where the app needs Rust-first APIs, stricter boundaries, or measured hot-path work. - In
portable, use the shippedhaxe.*andsys.*APIs.sys.io.File,sys.io.Process, andsys.net.Socketkeep Haxe behavior and may usehxrt. - In
metal, userust.*types when the source itself should be Rust-native. For example,rust.Vec.of([a, b])emitsvec![a, b]without a HaxeArray, andBorrow.withRef/Borrow.withMutgive scoped borrowed views that the compiler checks before it emits Rust. - If you must write awkward Haxe only to get good Rust output, report it. That is a compiler gap.
Read more: Profiles, Metal profile, and Portable vs metal authoring. Lifetime and RAII guides are in the Documentation Index.
cargo hx dev watches the project, compiles on each change, and runs the result. It needs
watchexec: brew install watchexec or
cargo install watchexec-cli.
cd examples/hello # or the folder of your own project
cargo hx devcargo hx dev --mode testruns the tests on each change.cargo hx dev --onceruns one cycle and does not needwatchexec.- Watch mode starts its own Haxe compile server and stops it on exit. The server saves Haxe's
parsing and typing. Each compile still generates Rust for the whole program. Pass
--no-haxe-serverto compile without the server.
More options: Dev Watcher guide.
- hello: smallest portable program; use it to check your toolchain
- chat_loopback: the main example, built in both profiles
- profile_storyboard: one small app written in each profile's style, with
@:rustTest - metal_first_dataflow: metal reference with
Result,Option, andVec - metal_native_net: metal TCP and UDP loopback without
hxrt - bytes_ops:
haxe.io.Bytesand Haxe-authored tests - async_retry_pipeline, classes, serde_json, tui_todo
- System APIs: sys_file_io, sys_net_loopback, sys_process, sys_thread_smoke, thread_pool_smoke
What each example covers: Examples Matrix.
| Task | Command |
|---|---|
| Snapshot tests | npm test |
| Full local harness, same as CI | npm run test:all |
| Remove generated files | npm run clean:artifacts:all |
| Compile-time report | npm run perf:compile-time |
| Refresh the generated summary in this README | npm run docs:sync:architecture-capability |
More commands are in AGENTS.md and Workflow.
-D rust_output=out: output directory for the generated Cargo project-D reflaxe_rust_profile=portable|metal: select the profile-D rust_no_build(alias-D rust_codegen_only): generate Rust and skip the Cargo build-D rust_build_release(alias-D rust_release): release build-D rust_target=<triple>: pass a target triple to Cargo-D rust_cargo_locked: pass--lockedto Cargo-D rust_async: enable the experimental async/await preview; see the Async/Await guide-D rustfmt: runcargo fmton the output (best effort; it invalidatesrust-source-map.json)
Full list: Defines reference.
Preferred order:
- Pure Haxe with the standard library and runtime APIs.
- Typed externs and metadata (
@:native,@:rustCargo,@:rustExtraSrc). - Framework wrappers around hand-written Rust modules.
Application code should not emit raw Rust with __rust__. It is a last resort for framework code,
and the examples and snapshot tests fail when they use it. See the Interop guide.
Rules for hand-written Rust helpers and for crates added by metadata are in the Native facade helper policy and the Support-crate facility (Apple Silicon macOS only).
- Readiness tracker: Compiler Progress Tracker
- Ongoing validation: Weekly CI Evidence
- Release process: Release docs
- Everything else, including historical records: Documentation Index
