To optimize Rust WASM binary size, compile in release mode with wasm-opt from the wasm-pack or wasm-bindgen-cli toolchain to strip debug symbols and dead code.
cargo build --target wasm32-unknown-unknown --release
wasm-opt -O4 target/wasm32-unknown-unknown/release/your_crate.wasm -o your_crate_optimized.wasm
Alternatively, configure your Cargo.toml to strip symbols directly during the build by adding a custom profile:
[profile.release]
opt-level = "s"
lto = true
strip = true
Then build with:
cargo build --release --target wasm32-unknown-unknown