简单地添加console_error_panic_hook::set_once()会导致错误:
[WARN]: :-) origin crate has no README
[INFO]: Installing wasm-bindgen...
error: cannot shadow already defined class `Error`
Error: Running the wasm-bindgen CLI
Caused by: failed to execute `wasm-bindgen`: exited with exit code: 1是的,我确实有自己的Error结构,但是为什么使用这个函数会导致“阴影”错误呢?
只有当我用Error导出[wasm_bindgen]结构时,才会发生错误。
运行wasm-pack build --target web后发生错误
再生产
# cargo.toml
[package]
name = "testing"
version = "0.1.0"
edition = "2021"
[lib]
crate-type = ["cdylib", "rlib"]
[dependencies]
console_error_panic_hook = "0.1.7"
wasm-bindgen = { version = "0.2.76" }// lib.rs
use wasm_bindgen::prelude::wasm_bindgen;
// Adding this does not help:
// extern crate console_error_panic_hook;
#[wasm_bindgen] // Works if either this line is remove...
struct Error {}
#[wasm_bindgen]
pub fn main() {
console_error_panic_hook::set_once(); // ... or this one
println!("Hello, world!");
}版本
wasm-pack 0.10.2
cargo 1.58.0 (f01b232bc 2022-01-19)
rustc 1.58.1 (db9d1b20b 2022-01-20)发布于 2022-02-10 03:32:18
此代码:
#[wasm_bindgen]
struct Error {}...means,您希望自己的struct Error被视为WASM-绑定到JavaScript Error type。由于console_error_panic_hook也需要绑定到JavaScript Error,这会产生冲突--使用多个绑定,当JavaScript将其Error传递给WASM函数时,还不清楚应该在Rust中创建什么结构。
https://stackoverflow.com/questions/71056057
复制相似问题