我如何将类型为 js_sys::JsString
的实例在 Rust WebAssembly 代码中转换为 &str
?
背景:我想将此 SO 回答中的代码转换为 Rust。我无法将js_sys::encode_uri_component
的输出作为值参数传递给web_sys::Element::set_attribute
:
let url = JsString::from("data:text/plain;charset=utf-8,");
url = url.concat(&js_sys::encode_uri_component(&text));
let anchor = document.create_element("a")
.and_then(|elm| elm.dyn_into::<web_sys::HtmlElement>())?;
anchor.set_attribute("href", &url)?; // Error: expected `str`, found struct `js_sys::JsString`
// ...
let x: String = url.into()
- Shepmaster