New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Accessing JS arrays from Rust #45
Comments
You can convert either a This should work: let array: Vec< String > = js! { return ["foo", "bar"]; }.try_into().unwrap(); You can't currently access a JavaScript array in Rust without converting it to a vector first. |
Thanks for the quick answer! I wasn't aware of the The above error seems to persist though: error[E0277]: the trait bound `std::vec::Vec<std::string::String>: std::convert::From<stdweb::Value>` is not satisfied
--> src/main.rs:25:63
|
25 | let array: Vec< String > = js! { return ["foo", "bar"]; }.try_into().unwrap();
| ^^^^^^^^ the trait `std::convert::From<stdweb::Value>` is not implemented for `std::vec::Vec<std::string::String>`
|
= help: the following implementations were found:
<std::vec::Vec<stdweb::Value> as std::convert::From<&'a stdweb::Array>>
<std::vec::Vec<u8> as std::convert::From<&'a stdweb::web::TypedArray<u8>>>
<std::vec::Vec<stdweb::Value> as std::convert::From<&'a mut stdweb::Array>>
<std::vec::Vec<stdweb::Value> as std::convert::From<stdweb::Array>>
and 10 others
= note: required because of the requirements on the impl of `std::convert::TryFrom<stdweb::Value>` for `std::vec::Vec<std::string::String>`
= note: required because of the requirements on the impl of `std::convert::TryInto<std::vec::Vec<std::string::String>>` for `stdweb::Value` Seems like I am missing a piece of the puzzle there ... |
You're using the wrong The |
That did the trick! This might as well be the next big thing for Rust ;) |
No problem; thanks. (: |
According to the documentation it is possible to convert a Rust Vector into JavaScript arrays.
I wasn't able to find any documentation on how to do that the other way around e.g. iterate a JS array or possibly convert it into a Rust vector if possible (obviously requires that all array entries can be converted to the same type).
Trying to pass an Array to Rust
I get the following error twice:
The text was updated successfully, but these errors were encountered: