“HashMap<i32, i32>”未实现特征“IntoWasmAbi”

Ani*_*rya 5 rust rust-cargo webassembly wasm-bindgen rust-wasm

尝试编译以下rust代码以wasm使其与现有js兼容运行。尝试从函数返回哈希映射值。

库文件

    use wasm_bindgen::prelude::*;
    use std::collections::HashMap;

   #[wasm_bindgen]
    pub fn get_transformed_filters()-> HashMap<i32, i32> { 
        let mut hm = HashMap::new();
        for i in 1..9990000 {
            hm.insert(i + i, i * i);
        }  
        return hm 
    }
Run Code Online (Sandbox Code Playgroud)

运行命令后控制台错误wasm-pack build

[INFO]:   Checking for the Wasm target...
[INFO]:   Compiling to Wasm...
   Compiling hello-wasm v0.1.0 (/Users/mfe/ui/rustService/test-wasm)
error[E0277]: the trait bound `HashMap<i32, i32>: IntoWasmAbi` is not satisfied
  --> src/lib.rs:15:1
   |
15 | #[wasm_bindgen]
   | ^^^^^^^^^^^^^^^ the trait `IntoWasmAbi` is not implemented for `HashMap<i32, i32>`
   |
   = note: required because of the requirements on the impl of `ReturnWasmAbi` for `HashMap<i32, i32>`
   = note: this error originates in an attribute macro (in Nightly builds, run with -Z macro-backtrace for more info)

error: aborting due to previous error

For more information about this error, try `rustc --explain E0277`.
error: could not compile `test-wasm`

To learn more, run the command again with --verbose.
Error: Compiling your crate to WebAssembly failed
Caused by: failed to execute `cargo build`: exited with exit code: 101
  full command: "cargo" "build" "--lib" "--release" "--target" "wasm32-unknown-unknown"
Run Code Online (Sandbox Code Playgroud)

有什么办法可以实现这一点吗?