41

I tried to make 'Hello World' in Rust using this tutorial, but the build command is a bit verbose:

cargo +nightly build --target wasm32-unknown-unknown --release

Is it possible to set the default target for cargo build?

antono
  • 978
  • 1
  • 7
  • 18

3 Answers3

77

You could use a Cargo configuration file to specify a default target-triple for your project. In your project's root, create a .cargo directory and a config.toml file in it with the following contents:

[build]
target = "wasm32-unknown-unknown"
citelao
  • 4,898
  • 2
  • 22
  • 36
mexus
  • 990
  • 6
  • 6
  • 3
    Is it work expectedly too now? I got the warning: `warning: unused manifest key: build` using 1.47.0-nightly. – Usagi Ito Aug 20 '20 at 12:19
  • 8
    Are you putting it in `Cargo.toml`?. You need to use `.cargo/config` – Jeff Muizelaar Sep 15 '20 at 21:45
  • 5
    Additional note: while you absolutely can call the file `config`, you can also call it `config.toml`. It will behave excactly the same, but your editor might now improve syntax highlighting and whatnot. – Zachary Perkins Aug 16 '22 at 03:54
  • 2
    @ZacharyPerkins: From what I know the `config.toml` filename with extension included is not supported for older versions of Cargo (released before ~2020). For current versions it is the preferred name, though. – blerontin Apr 17 '23 at 10:29
21

As listed in the Cargo documentation, you can create a .cargo/config and specify the target:

[build]
target = "my-custom-target"
Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
4

If you're able to use unstable features, following documentation and especially per-package-target feature add this to you crate manifest that usually named Cargo.toml

cargo-features = ["per-package-target"]

[package]
forced-target = "wasm32-unknown-unknown"
# and/or:
default-target = "wasm32-unknown-unknown"

This requires nightly toolchain.

Alex Koz.
  • 490
  • 8
  • 26