Skip to content

breaking change: How to call js functions from rust? #657

Closed Answered by huand
huand asked this question in Q&A
Discussion options

You must be logged in to vote

For info this is with trunk version 0.18.3

An easy an better way to do this is to use js snippet

so I would turn the file into a module, mark my function with export keyword and declare in rust

#[wasm_bindgen(module = "/utils.js")]
extern "C" {
    fn showALert(msg: &str);
}

#[wasm_bindgen]
pub fn greet(name: &str) {
    let message = format!("Hello, {}!", name);
    showALert(&message);
}

then a greet("Sam") would trigger the browser alert as intended!

Replies: 2 comments 2 replies

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
2 replies
@huand
Comment options

@huand
Comment options

Answer selected by huand
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
1 participant