我构建了一个简单的Rust应用程序,如下所示:
cargo new hello_world --bin
我可以使用Rust编译器编译到WebAssembly,如下所示:
rustc +nightly --target wasm32-unknown-unknown -O src/main.rs
我想使用Cargo来管理外部依赖项,但是当我使用cargo
为同一目标构建时:
cargo build --release --target wasm32-unknown-unknown
失败如下:
$ cargo build --release --target wasm32-unknown-unknown
error: failed to run `rustc` to learn about target-specific information
Caused by:
process didn't exit successfully: `rustc - --crate-name ___ --print=file-names --target wasm32-unknown-unknown --crate-type bin --crate-type rlib` (exit code: 101)
--- stderr
error: Error loading target specification: Could not find specification for target "wasm32-unknown-unknown"
|
= help: Use `--print target-list` for a list of built-in targets
答案 0 :(得分:2)
由于wasm32-unknown-unknown
目标最近才被添加到Rust中,因此它仅在夜间工具链中可用。您必须指定您希望使用夜间工具链:
cargo +nightly build --release --target wasm32-unknown-unknown
# ^^^^^^^^
您在直接致电rustc
时已经这样做了:
rustc +nightly --target wasm32-unknown-unknown -O src/main.rs
# ^^^^^^^^
如果你这样做可能会更容易:
在项目目录中使用工具链覆盖:
$ rustup override set nightly
在项目.cargo/config
中设置默认目标:
[build]
target = "wasm32-unknown-unknown"
然后你可以cargo build --release
。 (建议现在避免使用调试模式。)