Minimal WebAssembly without std



I have a project in WebAssembly, that I am currently writing with clang. I am not using any standard library (for example I have written the memory allocation myself). I would like to write some parts of this project with rust, and have a few questions regarding the state of WebAssembly in rust.

  • Is it possible to combine c and rust files to produce webassembly (maybe generate wasm32-unknown-unknown-wasm object files with rust, that can be linked with lld)?
  • I would like to have a fixed compiler version, that always produces the same output and is rarely changed. So using nightly is not possible. How do I produce a minimal wasm without std on a stable version of rust?

I experimented with the minimal example from hellorust, which produced a huge wasm file. Without std it is small, but I am not able to compile it with stable rust because of the feature annotation. This is my code:


use core::panic::PanicInfo;
use core::intrinsics;

pub fn add_one(x: i32) -> i32 {
    x + 1

#[lang = "panic_impl"]
extern fn rust_begin_panic(_info: &PanicInfo) -> ! 
    unsafe { intrinsics::abort() }


Have you tried to use lto=true for release build and wasm-gc? (and I think there was also a tool to strip unused panicking functionality, but I haven’t tried it yet) It should allow compiler to remove most of unused std functionality, in my case small project which uses vectors results IIRC in 13 KB uncompressed wasm binary, which I think should be tolerable for most use-cases.