Can I compile Rust to JavaScript (or another language) by way of WebAssembly?


#1

Here’s what I’m trying to do: write Rust code and have a tool that converts it into JavaScript or Python or another widespread language. The output can be entirely ugly and unreadable as long as it runs.

Why I want to do this is kind of banal - I want to be able to write Rust solutions to problems on sites like HackerRank and LeetCode (which don’t really support Rust) and run my solutions through their automated tests.

My thought was to compile Rust to WebAssembly and then find a tool that disassembles (?) wasm into a high-level language. I guess in theory I could write it myself, but it seems like something like this should already exist, especially for JavaScript. But I haven’t been able to find anything concrete using Google.

EDIT: I discovered that you can compile Rust code to LLVM .bc format and then use Emscripten to convert the .bc file to a JavaScript file. Still trying to get that process to work, so I’d still appreciate knowing if there’s a simpler way…


#2

There’s also a asmjs-unknown-emscripten target doing that for you already. It compiles to asm.js (a subset of JavaScript) using Emscripten.


#3

Thank you, I will check it out.