3

How can I convert an instance of type js_sys::JsString into &str within Rust WebAssembly code?

Background: I'd like to convert the code found in this SO answer into Rust. I 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`
// ...
Matthias Braun
  • 32,039
  • 22
  • 142
  • 171
blerontin
  • 2,892
  • 5
  • 34
  • 60

1 Answers1

3

As indicated by user Shepmaster in the question comments above the solution is that simple:

let x: String = url.into()
blerontin
  • 2,892
  • 5
  • 34
  • 60