小编mar*_*ina的帖子

使用 wasm-pack 构建时在 Rust 和 JavaScript 之间传递字符串

我正在构建一个 Chrome 扩展程序,并选择使用一些 WebAssembly 功能。我使用 wasm-pack 来构建源代码,因为它提供了--target web降低插入 Wasm 函数的复杂性的方法。在 Rust 和 JS 之间传递整数值可以无缝地工作,但我似乎无法将字符串传递给 Rust,反之亦然。

这是我正在处理的内容:

#[wasm_bindgen]
extern "C" {
    fn alert(s: &str);

    #[wasm_bindgen(js_namespace = console)]
    fn log(x: &str);
} 

#[wasm_bindgen]
pub extern "C" fn add_two(x: i32) -> i32 {
   x + 2
}

#[wasm_bindgen]
pub fn hello(name: &str) {
    log("Hello") // <-- passing a '&str' directly works. I can see it in the browser.
    log(name) // <-- does not seem to work. There is no output
    alert(&format!("Hello …
Run Code Online (Sandbox Code Playgroud)

rust rust-cargo webassembly wasm-bindgen wasm-pack

4
推荐指数
1
解决办法
1403
查看次数

标签 统计

rust ×1

rust-cargo ×1

wasm-bindgen ×1

wasm-pack ×1

webassembly ×1