2

I am trying to use a Web API that contains a method which accepts an array of strings from Rust.

I am using web_sys to "talk" to the JS API, but I can't find a way to pass in an array of static Strings into it.

In Rust, unfortunately, the type of the parameter is mistakenly declared as arg: &JsValue, so I can pass just about anything into it and it still compiles, but crashes in the browser.

How can I create an array of strings in Rust that can be used as a &JsValue?

2 Answers 2

2

This converts &[&str] to JsValue:

fn js_array(values: &[&str]) -> JsValue {
    return JsValue::from(values.into_iter()
        .map(|x| JsValue::from_str(x))
        .collect::<Array>());
}
Sign up to request clarification or add additional context in comments.

Comments

1

With the use of js_sys you can create arrays like this:

use js_sys::Array;

#[wasm_bindgen]
pub fn strings() -> Array {
    let arr = Array::new_with_length(10);
    for i in 0..arr.length() {
        let s = JsValue::from_str(&format!("str {}", i));
        arr.set(i, s);
    }
    arr
}

But can you give an example with string literals like ["hello"].to_array()

For the requested example you cannot use any method to convert directly. Therefore, you have to use a helper function:

#[wasm_bindgen]
pub fn strings() -> Array {
    to_array(&["str 1", "str 2"])
}

pub fn to_array(strings: &[&str] ) -> Array {

    let arr = Array::new_with_length(strings.len() as u32);
    for (i, s) in strings.iter().enumerate() {
        arr.set(i as u32, JsValue::from_str(s));
    }
    arr
}

6 Comments

Thanks! But can you give an example with string literals like ["hello"].to_array()?
Have a look at the new version
as I mention in the question, I need a &JsValue, not an Array so the code does not compile. Doing JsValue::from(array) does not seem to be working.
Could test your code with a changed version of the example: ``` #[wasm_bindgen] pub fn strings() -> JsValue { to_array(&["str 1", "str 2"]).into() } ```
@Ixx From the docs: “The utf-8 string provided is copied to the JS heap and the string will be owned by the JS garbage collector.” rustwasm.github.io/wasm-bindgen/api/wasm_bindgen/… So there is copying involved but no serialization.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.