hello_wasm/
lib.rs

1extern crate wasm_bindgen;
2
3use wasm_bindgen::prelude::*;
4
5#[wasm_bindgen]
6extern "C" {
7    pub fn alert(s: &str);
8}
9
10#[wasm_bindgen]
11pub fn greet(name: &str) {
12    alert(&format!("Hello, {}!", name));
13}
14use std::sync::{Arc, Mutex};
15use std::thread;
16
17#[wasm_bindgen]
18pub fn count() {
19    let counter = Arc::new(Mutex::new(0));
20    let mut handles = vec![];
21
22    for _ in 0..10 {
23        let counter = Arc::clone(&counter);
24        let handle = thread::spawn(move || {
25            let mut num = counter.lock().unwrap();
26
27            *num += 1;
28        });
29        handles.push(handle);
30    }
31
32    for handle in handles {
33        handle.join().unwrap();
34    }
35
36    println!("Result: {}", *counter.lock().unwrap());
37    // counter.lock().unwrap()
38}
39
40#[cfg(test)]
41mod tests {
42    use super::*;
43    #[test]
44    fn test_count() {
45        count();
46        assert!(true);
47    }
48}