如何在 Rust WASM 中将 JsString 转换为 &str

How can I convert JsString into &str in Rust WASM

如何在 Rust WebAssembly 代码中将类型 js_sys::JsString 的实例转换为 &str

背景:我想转换在this SO answer into Rust and struggle to pass the output of js_sys::encode_uri_component as the value parameter to 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`
// ...

正如用户 Shepmaster 在上面的问题评论中指出的那样,解决方案就是这么简单:

let x: String = url.into()