Detect browser target

How to detect browser target? The language reference shows the following attribute:

  • #[cfg(target_family = "wasm")]

But the problem is that WebAssembly is not limited to browser. Is that correct?

Currently there is no way to distinguish a browser from other wasm environments solely based on cfg. In practice, a lot of code assumes the wasm32-unknown-unknown target is a browser, but that's technically wrong.

The problem is that "is a browser" isn't actually enough information to start invoking web APIs; all current strategies rely on additional JS code providing adapters (such as that generated by wasm-bindgen). In principle there could be a wasm32-unknown-wasmbindgen target triple, but it's a little odd to promote a specific Rust tool into being a target triple, and it hasn't been done.

1 Like

So there are no plans on providing a new target triple specifically for web browsers?

Not no plans like "we don't want to do this", but no plans like: if it is going to happen then someone's got to write the RFC, get it accepted, submit patches to all the important libraries, and so on.

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.