Debian Codegraph @

.cargo/vendor/wasm-bindgen/guide/src/reference/rust-targets.md

raw
1 # Supported Rust Targets
2
3 > **Note**: This section is about Rust target triples, not targets like node/web
4 > workers/browsers. More information on that coming soon!
5
6 The `wasm-bindgen` project is designed to target the `wasm32-unknown-unknown`
7 target in Rust. This target is a "bare bones" target for Rust which emits
8 WebAssembly as output. The standard library is largely inert as modules like
9 `std::fs` and `std::net` will simply return errors.
10
11 ## Non-wasm targets
12
13 Note that `wasm-bindgen` also aims to compile on all targets. This means that it
14 should be safe, if you like, to use `#[wasm_bindgen]` even when compiling for
15 Windows (for example). For example:
16
17 ```rust
18 #[wasm_bindgen]
19 pub fn add(a: u32, b: u32) -> u32 {
20 a + b
21 }
22
23 #[cfg(not(target_arch = "wasm32"))]
24 fn main() {
25 println!("1 + 2 = {}", add(1, 2));
26 }
27 ```
28
29 This program will compile and work on all platforms, not just
30 `wasm32-unknown-unknown`. Note that imported functions with `#[wasm_bindgen]`
31 will unconditionally panic on non-wasm targets. For example:
32
33 ```rust
34 #[wasm_bindgen]
35 extern "C" {
36 #[wasm_bindgen(js_namespace = console)]
37 fn log(s: &str);
38 }
39
40 fn main() {
41 log("hello!");
42 }
43 ```
44
45 This program will unconditionally panic on all platforms other than
46 `wasm32-unknown-unknown`.
47
48 For better compile times, however, you likely want to only use `#[wasm_bindgen]`
49 on the `wasm32-unknown-unknown` target. You can have a target-specific
50 dependency like so:
51
52 ```toml
53 [target.'cfg(target_arch = "wasm32")'.dependencies]
54 wasm-bindgen = "0.2"
55 ```
56
57 And in your code you can use:
58
59 ```rust
60 #[cfg(target_arch = "wasm32")]
61 #[wasm_bindgen]
62 pub fn only_on_the_wasm_target() {
63 // ...
64 }
65 ```
66
67 ## Other Web Targets
68
69 The `wasm-bindgen` target does not support the `wasm32-unknown-emscripten` nor
70 the `asmjs-unknown-emscripten` targets. There are currently no plans to support
71 these targets either. All annotations work like other platforms on the targets,
72 retaining exported functions and causing all imports to panic.