代码是基本的,我正在用lib.rs中声明的字符串输入从js调用一个函数 但是该字符串未传递给wasm中的函数,我收到一个空字符串,反之亦不起作用,我也无法将字符串从wasm传递给js。
wasm文件如下
use wasm_bindgen::prelude::*;
use web_sys::console;
// When the `wee_alloc` feature is enabled, use `wee_alloc` as the global
// allocator.
#[cfg(feature = "wee_alloc")]
#[global_allocator]
static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT;
#[wasm_bindgen]
pub fn hello(v: String) {
console::log_1(&v.into()); //prints => empty string
}
js好像wasm已经成功启动,其他功能也正常工作
wasm.hello("hello")
货运toml看起来像这样
[package]
name = "painter"
version = "0.1.0"
edition = "2018"
[lib]
crate-type = ["cdylib", "rlib"]
[features]
default = ["console_error_panic_hook"]
[dependencies]
wasm-bindgen = "0.2"
#web-sys = "0.3.39"
# The `console_error_panic_hook` crate provides better debugging of panics by
# logging them with `console.error`. This is great for development, but requires
# all the `std::fmt` and `std::panicking` infrastructure, so isn't great for
# code size when deploying.
console_error_panic_hook = { version = "0.1.1", optional = true }
[dependencies.web-sys]
version = "0.3.39"
features = [
"console",
"Document",
"Element",
"HtmlElement",
"Node",
"Window"
] # Do you have this line in your Cargo.toml?
# `wee_alloc` is a tiny allocator for wasm that is only ~1K in code size
# compared to the default allocator's ~10K. It is slower than the default
# allocator, however.
#
# Unfortunately, `wee_alloc` requires nightly Rust when targeting wasm for now.
wee_alloc = { version = "0.4.2", optional = true }
[dev-dependencies]
wasm-bindgen-test = "0.2"
[profile.release]
# Tell `rustc` to optimize for small code size.
opt-level = "s"
答案 0 :(得分:1)
好吧,我发现我使用的是wasm init的导出,而不是通过wasm-pack生成的js文件的导出,因此在使用wasm-pack导出时请记住这一点,并在启动js文件