Skip to content

Instantly share code, notes, and snippets.

@arun-gupta
Last active June 15, 2023 11:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arun-gupta/cd4c1043611aeee5b89c8b5a21c2ba65 to your computer and use it in GitHub Desktop.
Save arun-gupta/cd4c1043611aeee5b89c8b5a21c2ba65 to your computer and use it in GitHub Desktop.
Create a Rust-based Web Assembly application
  • Install rustup and the latest version of Rust

    arungupt@Aruns-MBP-52212 ~ % curl --proto '=https' --tlsv1.2 https://sh.rustup.rs -sSf | sh
    info: downloading installer
    
    Welcome to Rust!
    
    This will download and install the official compiler for the Rust
    programming language, and its package manager, Cargo.
    
    Rustup metadata and toolchains will be installed into the Rustup
    home directory, located at:
    
      /Users/arungupt/.rustup
    
    This can be modified with the RUSTUP_HOME environment variable.
    
    The Cargo home directory is located at:
    
      /Users/arungupt/.cargo
    
    This can be modified with the CARGO_HOME environment variable.
    
    The cargo, rustc, rustup and other commands will be added to
    Cargo's bin directory, located at:
    
      /Users/arungupt/.cargo/bin
    
    This path will then be added to your PATH environment variable by
    modifying the profile files located at:
    
      /Users/arungupt/.profile
      /Users/arungupt/.zshenv
    
    You can uninstall at any time with rustup self uninstall and
    these changes will be reverted.
    
    Current installation options:
    
    
       default host triple: aarch64-apple-darwin
         default toolchain: stable (default)
                   profile: default
      modify PATH variable: yes
    
    1) Proceed with installation (default)
    2) Customize installation
    3) Cancel installation
    >
    
    info: profile set to 'default'
    info: default host triple is aarch64-apple-darwin
    info: syncing channel updates for 'stable-aarch64-apple-darwin'
    732.1 KiB / 732.1 KiB (100 %) 543.4 KiB/s in  1s ETA:  0s
    info: latest update on 2023-06-01, rust version 1.70.0 (90c541806 2023-05-31)
    info: downloading component 'cargo'
      4.9 MiB /   4.9 MiB (100 %)   2.2 MiB/s in  2s ETA:  0s
    info: downloading component 'clippy'
      1.9 MiB /   1.9 MiB (100 %)   1.9 MiB/s in  1s ETA:  0s
    info: downloading component 'rust-docs'
     13.5 MiB /  13.5 MiB (100 %)   2.7 MiB/s in  5s ETA:  0s
    info: downloading component 'rust-std'
     25.5 MiB /  25.5 MiB (100 %)   2.8 MiB/s in 12s ETA:  0s
    info: downloading component 'rustc'
     52.6 MiB /  52.6 MiB (100 %)   2.5 MiB/s in 25s ETA:  0s
    info: downloading component 'rustfmt'
    info: installing component 'cargo'
    info: installing component 'clippy'
    info: installing component 'rust-docs'
     13.5 MiB /  13.5 MiB (100 %)   5.0 MiB/s in  1s ETA:  0s
    info: installing component 'rust-std'
     25.5 MiB /  25.5 MiB (100 %)  18.3 MiB/s in  1s ETA:  0s
    info: installing component 'rustc'
     52.6 MiB /  52.6 MiB (100 %)  21.9 MiB/s in  2s ETA:  0s
    info: installing component 'rustfmt'
    info: default toolchain set to 'stable-aarch64-apple-darwin'
    
      stable-aarch64-apple-darwin installed - rustc 1.70.0 (90c541806 2023-05-31)
    
    
    Rust is installed now. Great!
    
    To get started you may need to restart your current shell.
    This would reload your PATH environment variable to include
    Cargo's bin directory ($HOME/.cargo/bin).
    
    To configure your current shell, run:
    source "$HOME/.cargo/env"
    
  • Create Rust program

    mkdir projects 
    cd projects 
    mkdir hello_world 
    cd hello_world 
    vi main.rs
    

    Add the following code:

    fn main() {
        println!("Hello, world!");
    }
    
  • Compile and run the code:

    arungupt@Aruns-MBP-52212 hello_world % rustc main.rs 
    arungupt@Aruns-MBP-52212 hello_world % ./main  
    
    Hello, world! 
    
  • Install wasm-pack

    arungupt@Aruns-MBP-52212 hello_world % cargo install wasm-pack
    
        Updating crates.io index
      Downloaded wasm-pack v0.11.1
      Downloaded 1 crate (426.1 KB) in 22.78s
      Installing wasm-pack v0.11.1
        Updating crates.io index
      Downloaded tinyvec_macros v0.1.1
      Downloaded anstyle v1.0.0
      Downloaded mime v0.3.17
      Downloaded try-lock v0.2.4
      Downloaded want v0.3.0
      Downloaded atty v0.2.14
      Downloaded constant_time_eq v0.1.5
      Downloaded time-core v0.1.1
      Downloaded tower-service v0.3.2
      Downloaded colorchoice v1.0.0
      Downloaded jobserver v0.1.26
      Downloaded anstyle-parse v0.2.0
      Downloaded fastrand v1.9.0
      Downloaded foreign-types v0.3.2
      Downloaded foreign-types-shared v0.1.1
      Downloaded pin-utils v0.1.0
      Downloaded openssl-macros v0.1.1
      Downloaded proc-macro-error-attr v1.0.4
      Downloaded which v4.4.0
      Downloaded serde_spanned v0.6.2
      Downloaded dirs-sys-next v0.1.2
      Downloaded errno v0.3.1
      Downloaded form_urlencoded v1.2.0
      Downloaded futures-sink v0.3.28
      Downloaded futures-io v0.3.28
      Downloaded inout v0.1.3
      Downloaded crypto-common v0.1.6
      Downloaded is_executable v0.1.2
      Downloaded is-terminal v0.4.7
      Downloaded vec_map v0.8.2
      Downloaded xattr v0.2.3
      Downloaded utf8parse v0.2.1
      Downloaded anstyle-query v1.0.0
      Downloaded serde_ignored v0.1.7
      Downloaded shell-words v1.1.0
      Downloaded cpufeatures v0.2.7
      Downloaded fnv v1.0.7
      Downloaded lazy_static v1.4.0
      Downloaded futures-task v0.3.28
      Downloaded hex v0.4.3
      Downloaded block-buffer v0.10.4
      Downloaded httpdate v1.0.2
      Downloaded hyper-tls v0.5.0
      Downloaded itoa v1.0.6
      Downloaded toml_datetime v0.6.2
      Downloaded cfg-if v1.0.0
      Downloaded adler v1.0.2
      Downloaded cargo-platform v0.1.2
      Downloaded filetime v0.2.21
      Downloaded heck v0.3.3
      Downloaded http-body v0.4.5
      Downloaded fs2 v0.4.3
      Downloaded human-panic v1.1.4
      Downloaded percent-encoding v2.3.0
      Downloaded version_check v0.9.4
      Downloaded zstd-safe v5.0.2+zstd.1.5.2
      Downloaded strsim v0.10.0
      Downloaded scopeguard v1.1.0
      Downloaded same-file v1.0.6
      Downloaded sha1 v0.10.5
      Downloaded strsim v0.8.0
      Downloaded tokio-native-tls v0.3.1
      Downloaded siphasher v0.3.10
      Downloaded binary-install v0.1.0
      Downloaded thiserror-impl v1.0.40
      Downloaded pbkdf2 v0.11.0
      Downloaded serde_urlencoded v0.7.1
      Downloaded autocfg v1.1.0
      Downloaded dirs-next v2.0.0
      Downloaded futures-core v0.3.28
      Downloaded unicode-width v0.1.10
      Downloaded byteorder v1.4.3
      Downloaded either v1.8.1
      Downloaded subtle v2.5.0
      Downloaded zeroize v1.6.0
      Downloaded generic-array v0.14.7
      Downloaded num_cpus v1.15.0
      Downloaded anstream v0.3.2
      Downloaded slab v0.4.8
      Downloaded bitflags v1.3.2
      Downloaded cargo_metadata v0.15.4
      Downloaded pkg-config v0.3.27
      Downloaded iana-time-zone v0.1.57
      Downloaded lock_api v0.4.10
      Downloaded cipher v0.4.4
      Downloaded tinyvec v1.6.0
      Downloaded glob v0.3.1
      Downloaded core-foundation-sys v0.8.4
      Downloaded walkdir v2.3.3
      Downloaded core-foundation v0.9.3
      Downloaded security-framework-sys v2.9.0
      Downloaded textwrap v0.11.0
      Downloaded structopt-derive v0.4.18
      Downloaded digest v0.10.7
      Downloaded rand_core v0.6.4
      Downloaded ipnet v2.7.2
      Downloaded thiserror v1.0.40
      Downloaded zstd v0.11.2+zstd.1.5.2
      Downloaded addr2line v0.19.0
      Downloaded password-hash v0.4.2
      Downloaded os_info v3.7.0
      Downloaded camino v1.1.4
      Downloaded native-tls v0.2.11
      Downloaded sha2 v0.10.6
      Downloaded futures-channel v0.3.28
      Downloaded pin-project-lite v0.2.9
      Downloaded ansi_term v0.12.1
      Downloaded quote v1.0.28
      Downloaded typenum v1.16.0
      Downloaded rustc-demangle v0.1.23
      Downloaded smallvec v1.10.0
      Downloaded time v0.1.45
      Downloaded httparse v1.8.0
      Downloaded anyhow v1.0.71
      Downloaded bzip2 v0.4.4
      Downloaded getrandom v0.2.10
      Downloaded once_cell v1.18.0
      Downloaded proc-macro-error v1.0.4
      Downloaded semver v1.0.17
      Downloaded tempfile v3.6.0
      Downloaded unicode-ident v1.0.9
      Downloaded unicode-bidi v0.3.13
      Downloaded dialoguer v0.10.4
      Downloaded console v0.15.7
      Downloaded env_logger v0.10.0
      Downloaded parking_lot_core v0.9.8
      Downloaded base64ct v1.6.0
      Downloaded uuid v1.3.3
      Downloaded toml v0.7.4
      Downloaded crc32fast v1.3.2
      Downloaded miniz_oxide v0.6.2
      Downloaded miniz_oxide v0.7.1
      Downloaded io-lifetimes v1.0.11
      Downloaded indexmap v1.9.3
      Downloaded num-traits v0.2.15
      Downloaded log v0.4.19
      Downloaded proc-macro2 v1.0.60
      Downloaded toml v0.5.11
      Downloaded hmac v0.12.1
      Downloaded bytes v1.4.0
      Downloaded parking_lot v0.12.1
      Downloaded tracing-core v0.1.31
      Downloaded socket2 v0.4.9
      Downloaded ryu v1.0.13
      Downloaded memchr v2.5.0
      Downloaded zip v0.6.6
      Downloaded tar v0.4.38
      Downloaded openssl-sys v0.9.88
      Downloaded tracing v0.1.37
      Downloaded structopt v0.3.26
      Downloaded url v2.4.0
      Downloaded backtrace v0.3.67
      Downloaded serde_derive v1.0.164
      Downloaded cc v1.0.79
      Downloaded toml_edit v0.19.10
      Downloaded unicode-segmentation v1.10.1
      Downloaded tokio-util v0.7.8
      Downloaded base64 v0.21.2
      Downloaded flate2 v1.0.26
      Downloaded serde v1.0.164
      Downloaded security-framework v2.9.1
      Downloaded unicode-normalization v0.1.22
      Downloaded aes v0.8.2
      Downloaded winnow v0.4.6
      Downloaded curl v0.4.44
      Downloaded http v0.2.9
      Downloaded mio v0.8.8
      Downloaded futures-util v0.3.28
      Downloaded hashbrown v0.12.3
      Downloaded chrono v0.4.26
      Downloaded time v0.3.22
      Downloaded hyper v0.14.26
      Downloaded reqwest v0.11.18
      Downloaded serde_json v1.0.96
      Downloaded h2 v0.3.19
      Downloaded openssl v0.10.54
      Downloaded idna v0.4.0
      Downloaded clap v2.34.0
      Downloaded syn v1.0.109
      Downloaded syn v2.0.18
      Downloaded object v0.30.4
      Downloaded rustix v0.37.20
      Downloaded bzip2-sys v0.1.11+1.0.8
      Downloaded tokio v1.28.2
    warning: spurious network error (3 tries remaining): [92] Stream error in the HTTP/2 framing layer (HTTP/2 stream 0 was not closed cleanly: PROTOCOL_ERROR (err 1))
      Downloaded gimli v0.27.2
      Downloaded zstd-sys v2.0.8+zstd.1.5.5
    warning: spurious network error (3 tries remaining): [18] Transferred a partial file (transfer closed with 1811969 bytes remaining to read)
    warning: spurious network error (3 tries remaining): [18] Transferred a partial file (transfer closed with 761862 bytes remaining to read)
      Downloaded libc v0.2.146
    warning: spurious network error (3 tries remaining): [92] Stream error in the HTTP/2 framing layer (HTTP/2 stream 0 was not closed cleanly: PROTOCOL_ERROR (err 1))
      Downloaded encoding_rs v0.8.32
      Downloaded libz-sys v1.1.9
      Downloaded curl-sys v0.4.63+curl-8.1.2
      Downloaded openssl-src v111.26.0+1.1.1u
      Downloaded 191 crates (23.6 MB) in 15m 52s (largest was `openssl-src` at 5.1 MB)
       Compiling libc v0.2.146
       Compiling autocfg v1.1.0
       Compiling proc-macro2 v1.0.60
       Compiling quote v1.0.28
       Compiling unicode-ident v1.0.9
       Compiling cfg-if v1.0.0
       Compiling pkg-config v0.3.27
       Compiling version_check v0.9.4
       Compiling serde v1.0.164
       Compiling typenum v1.16.0
       Compiling bitflags v1.3.2
       Compiling io-lifetimes v1.0.11
       Compiling generic-array v0.14.7
       Compiling rustix v0.37.20
       Compiling memchr v2.5.0
       Compiling pin-project-lite v0.2.9
       Compiling indexmap v1.9.3
       Compiling itoa v1.0.6
       Compiling futures-core v0.3.28
       Compiling core-foundation-sys v0.8.4
       Compiling bytes v1.4.0
       Compiling tokio v1.28.2
       Compiling jobserver v0.1.26
       Compiling hashbrown v0.12.3
       Compiling cc v1.0.79
       Compiling once_cell v1.18.0
       Compiling subtle v2.5.0
       Compiling libz-sys v1.1.9
       Compiling errno v0.3.1
       Compiling crypto-common v0.1.6
       Compiling socket2 v0.4.9
       Compiling block-buffer v0.10.4
       Compiling mio v0.8.8
       Compiling num_cpus v1.15.0
       Compiling zstd-sys v2.0.8+zstd.1.5.5
       Compiling syn v2.0.18
       Compiling slab v0.4.8
       Compiling tempfile v3.6.0
       Compiling futures-task v0.3.28
       Compiling adler v1.0.2
       Compiling lazy_static v1.4.0
       Compiling log v0.4.19
       Compiling digest v0.10.7
       Compiling cpufeatures v0.2.7
       Compiling tracing-core v0.1.31
       Compiling fnv v1.0.7
       Compiling futures-util v0.3.28
       Compiling fastrand v1.9.0
       Compiling tracing v0.1.37
       Compiling http v0.2.9
       Compiling core-foundation v0.9.3
       Compiling security-framework-sys v2.9.0
       Compiling curl-sys v0.4.63+curl-8.1.2
       Compiling bzip2-sys v0.1.11+1.0.8
       Compiling openssl-src v111.26.0+1.1.1u
       Compiling serde_derive v1.0.164
       Compiling proc-macro-error-attr v1.0.4
       Compiling crc32fast v1.3.2
       Compiling pin-utils v0.1.0
       Compiling zstd-safe v5.0.2+zstd.1.5.2
       Compiling native-tls v0.2.11
       Compiling futures-sink v0.3.28
       Compiling syn v1.0.109
       Compiling unicode-width v0.1.10
       Compiling tinyvec_macros v0.1.1
       Compiling futures-channel v0.3.28
       Compiling httparse v1.8.0
       Compiling futures-io v0.3.28
       Compiling tinyvec v1.6.0
       Compiling tokio-util v0.7.8
       Compiling openssl-sys v0.9.88
       Compiling security-framework v2.9.1
       Compiling inout v0.1.3
       Compiling proc-macro-error v1.0.4
       Compiling try-lock v0.2.4
       Compiling ryu v1.0.13
       Compiling percent-encoding v2.3.0
       Compiling base64ct v1.6.0
       Compiling rand_core v0.6.4
       Compiling form_urlencoded v1.2.0
       Compiling password-hash v0.4.2
       Compiling want v0.3.0
       Compiling cipher v0.4.4
       Compiling h2 v0.3.19
       Compiling unicode-normalization v0.1.22
       Compiling http-body v0.4.5
       Compiling sha2 v0.10.6
       Compiling hmac v0.12.1
       Compiling miniz_oxide v0.7.1
       Compiling backtrace v0.3.67
       Compiling lock_api v0.4.10
       Compiling num-traits v0.2.15
       Compiling gimli v0.27.2
       Compiling tower-service v0.3.2
       Compiling thiserror v1.0.40
       Compiling unicode-bidi v0.3.13
       Compiling serde_spanned v0.6.2
       Compiling toml_datetime v0.6.2
       Compiling camino v1.1.4
       Compiling semver v1.0.17
       Compiling unicode-segmentation v1.10.1
       Compiling anyhow v1.0.71
       Compiling parking_lot_core v0.9.8
       Compiling time-core v0.1.1
       Compiling httpdate v1.0.2
       Compiling curl v0.4.44
       Compiling serde_json v1.0.96
       Compiling winnow v0.4.6
       Compiling utf8parse v0.2.1
       Compiling anstyle-parse v0.2.0
       Compiling addr2line v0.19.0
       Compiling hyper v0.14.26
       Compiling heck v0.3.3
       Compiling time v0.3.22
       Compiling toml_edit v0.19.10
       Compiling idna v0.4.0
       Compiling flate2 v1.0.26
       Compiling pbkdf2 v0.11.0
       Compiling aes v0.8.2
       Compiling tokio-native-tls v0.3.1
       Compiling textwrap v0.11.0
       Compiling thiserror-impl v1.0.40
       Compiling sha1 v0.10.5
       Compiling miniz_oxide v0.6.2
       Compiling is-terminal v0.4.7
       Compiling object v0.30.4
       Compiling xattr v0.2.3
       Compiling atty v0.2.14
       Compiling getrandom v0.2.10
       Compiling filetime v0.2.21
       Compiling dirs-sys-next v0.1.2
       Compiling anstyle v1.0.0
       Compiling byteorder v1.4.3
       Compiling vec_map v0.8.2
       Compiling scopeguard v1.1.0
       Compiling strsim v0.8.0
       Compiling anstyle-query v1.0.0
       Compiling colorchoice v1.0.0
       Compiling foreign-types-shared v0.1.1
       Compiling ansi_term v0.12.1
       Compiling smallvec v1.10.0
       Compiling constant_time_eq v0.1.5
       Compiling openssl v0.10.54
       Compiling rustc-demangle v0.1.23
       Compiling anstream v0.3.2
       Compiling clap v2.34.0
       Compiling foreign-types v0.3.2
       Compiling dirs-next v2.0.0
       Compiling tar v0.4.38
       Compiling uuid v1.3.3
       Compiling hyper-tls v0.5.0
       Compiling structopt-derive v0.4.18
       Compiling url v2.4.0
       Compiling toml v0.7.4
       Compiling serde_urlencoded v0.7.1
       Compiling os_info v3.7.0
       Compiling cargo-platform v0.1.2
       Compiling console v0.15.7
       Compiling openssl-macros v0.1.1
       Compiling time v0.1.45
       Compiling fs2 v0.4.3
       Compiling iana-time-zone v0.1.57
       Compiling encoding_rs v0.8.32
       Compiling siphasher v0.3.10
       Compiling zeroize v1.6.0
       Compiling is_executable v0.1.2
       Compiling either v1.8.1
       Compiling base64 v0.21.2
       Compiling shell-words v1.1.0
       Compiling same-file v1.0.6
       Compiling hex v0.4.3
       Compiling ipnet v2.7.2
       Compiling mime v0.3.17
       Compiling walkdir v2.3.3
       Compiling dialoguer v0.10.4
       Compiling which v4.4.0
       Compiling reqwest v0.11.18
       Compiling chrono v0.4.26
       Compiling human-panic v1.1.4
       Compiling cargo_metadata v0.15.4
       Compiling structopt v0.3.26
       Compiling parking_lot v0.12.1
       Compiling serde_ignored v0.1.7
       Compiling toml v0.5.11
       Compiling env_logger v0.10.0
       Compiling glob v0.3.1
       Compiling strsim v0.10.0
       Compiling bzip2 v0.4.4
       Compiling zstd v0.11.2+zstd.1.5.2
       Compiling zip v0.6.6
       Compiling binary-install v0.1.0
       Compiling wasm-pack v0.11.1
        Finished release [optimized] target(s) in 17m 15s
      Installing /Users/arungupt/.cargo/bin/wasm-pack
       Installed package `wasm-pack v0.11.1` (executable `wasm-pack`)
    
  • Build the wasm package

    arungupt@Aruns-MBP-52212 hello-wasm % wasm-pack build --target web
    [INFO]: 🎯  Checking for the Wasm target...
    info: downloading component 'rust-std' for 'wasm32-unknown-unknown'
    info: installing component 'rust-std' for 'wasm32-unknown-unknown'
    [INFO]: 🌀  Compiling to Wasm...
       Compiling proc-macro2 v1.0.60
       Compiling quote v1.0.28
       Compiling unicode-ident v1.0.9
       Compiling wasm-bindgen-shared v0.2.86
       Compiling bumpalo v3.13.0
       Compiling log v0.4.19
       Compiling once_cell v1.18.0
       Compiling wasm-bindgen v0.2.86
       Compiling cfg-if v1.0.0
       Compiling syn v2.0.18
       Compiling wasm-bindgen-backend v0.2.86
       Compiling wasm-bindgen-macro-support v0.2.86
       Compiling wasm-bindgen-macro v0.2.86
       Compiling hello-wasm v0.1.0 (/Users/arungupt/projects/hello-wasm)
        Finished release [optimized] target(s) in 6.85s
    [WARN]: ⚠️   origin crate has no README
    [INFO]: License key is set in Cargo.toml but no LICENSE file(s) were found; Please add the LICENSE file(s) to your project directory
    [INFO]: ⬇️  Installing wasm-bindgen...
        Updating crates.io index
      Downloaded wasm-bindgen-cli v0.2.86
      Downloaded 1 crate (41.7 KB) in 1.95s
      Installing wasm-bindgen-cli v0.2.86
        Updating crates.io index
      Downloaded safemem v0.3.3
      Downloaded wasm-bindgen-wasm-interpreter v0.2.86
      Downloaded num_threads v0.1.6
      Downloaded chunked_transfer v1.4.1
      Downloaded id-arena v2.2.1
      Downloaded wasm-bindgen-wasm-conventions v0.2.86
      Downloaded twoway v0.1.8
      Downloaded buf_redux v0.8.4
      Downloaded wasm-bindgen-externref-xform v0.2.86
      Downloaded wasm-bindgen-multi-value-xform v0.2.86
      Downloaded walrus-macro v0.19.0
      Downloaded quick-error v1.2.3
      Downloaded threadpool v1.8.1
      Downloaded leb128 v0.2.5
      Downloaded ppv-lite86 v0.2.17
      Downloaded memoffset v0.9.0
      Downloaded crossbeam-deque v0.8.3
      Downloaded docopt v1.1.1
      Downloaded base64 v0.9.3
      Downloaded termcolor v1.2.0
      Downloaded humantime v2.1.0
      Downloaded rand_chacha v0.3.1
      Downloaded sha1_smol v1.0.0
      Downloaded base64 v0.13.1
      Downloaded unicase v2.6.0
      Downloaded mime_guess v2.0.4
      Downloaded ascii v1.1.0
      Downloaded tiny_http v0.12.0
      Downloaded multipart v0.18.0
      Downloaded rouille v3.6.2
      Downloaded crossbeam-utils v0.8.16
      Downloaded wasmparser v0.77.1
      Downloaded env_logger v0.8.4
      Downloaded crossbeam-epoch v0.9.15
      Downloaded rayon-core v1.11.0
      Downloaded wasm-bindgen-cli-support v0.2.86
      Downloaded rand v0.8.5
      Downloaded crossbeam-channel v0.5.8
      Downloaded walrus v0.19.0
      Downloaded rayon v1.7.0
      Downloaded aho-corasick v1.0.2
      Downloaded wasm-bindgen-threads-xform v0.2.86
      Downloaded regex-syntax v0.7.2
      Downloaded regex v1.8.4
      Downloaded 44 crates (2.4 MB) in 1.77s
       Compiling libc v0.2.146
       Compiling autocfg v1.1.0
       Compiling cfg-if v1.0.0
       Compiling proc-macro2 v1.0.60
       Compiling quote v1.0.28
       Compiling unicode-ident v1.0.9
       Compiling crossbeam-utils v0.8.16
       Compiling scopeguard v1.1.0
       Compiling rayon-core v1.11.0
       Compiling log v0.4.19
       Compiling version_check v0.9.4
       Compiling memchr v2.5.0
       Compiling syn v1.0.109
       Compiling memoffset v0.9.0
       Compiling crossbeam-epoch v0.9.15
       Compiling unicase v2.6.0
       Compiling anyhow v1.0.71
       Compiling either v1.8.1
       Compiling unicode-segmentation v1.10.1
       Compiling heck v0.3.3
       Compiling cc v1.0.79
       Compiling pkg-config v0.3.27
       Compiling io-lifetimes v1.0.11
       Compiling num_cpus v1.15.0
       Compiling leb128 v0.2.5
       Compiling wasmparser v0.77.1
       Compiling libz-sys v1.1.9
       Compiling syn v2.0.18
       Compiling crossbeam-channel v0.5.8
       Compiling serde v1.0.164
       Compiling rustix v0.37.20
       Compiling errno v0.3.1
       Compiling crossbeam-deque v0.8.3
       Compiling serde_derive v1.0.164
       Compiling getrandom v0.2.10
       Compiling rayon v1.7.0
       Compiling tempfile v3.6.0
       Compiling bitflags v1.3.2
       Compiling tinyvec_macros v0.1.1
       Compiling safemem v0.3.3
       Compiling walrus-macro v0.19.0
       Compiling tinyvec v1.6.0
       Compiling rand_core v0.6.4
       Compiling mime_guess v2.0.4
       Compiling id-arena v2.2.1
       Compiling curl-sys v0.4.63+curl-8.1.2
       Compiling num-traits v0.2.15
       Compiling httparse v1.8.0
       Compiling walrus v0.19.0
       Compiling serde_json v1.0.96
       Compiling ppv-lite86 v0.2.17
       Compiling fastrand v1.9.0
       Compiling rand_chacha v0.3.1
       Compiling unicode-normalization v0.1.22
       Compiling aho-corasick v1.0.2
       Compiling unicode-bidi v0.3.13
       Compiling wasm-bindgen-wasm-conventions v0.2.86
       Compiling itoa v1.0.6
       Compiling mime v0.3.17
       Compiling regex-syntax v0.7.2
       Compiling core-foundation-sys v0.8.4
       Compiling ryu v1.0.13
       Compiling wasm-bindgen-shared v0.2.86
       Compiling percent-encoding v2.3.0
       Compiling form_urlencoded v1.2.0
       Compiling iana-time-zone v0.1.57
       Compiling idna v0.4.0
       Compiling rand v0.8.5
       Compiling regex v1.8.4
       Compiling buf_redux v0.8.4
       Compiling twoway v0.1.8
       Compiling num_threads v0.1.6
       Compiling chunked_transfer v1.4.1
       Compiling httpdate v1.0.2
       Compiling time-core v0.1.1
       Compiling byteorder v1.4.3
       Compiling curl v0.4.44
       Compiling ascii v1.1.0
       Compiling quick-error v1.2.3
       Compiling time v0.3.22
       Compiling multipart v0.18.0
       Compiling tiny_http v0.12.0
       Compiling base64 v0.9.3
       Compiling chrono v0.4.26
       Compiling url v2.4.0
       Compiling wasm-bindgen-wasm-interpreter v0.2.86
       Compiling wasm-bindgen-threads-xform v0.2.86
       Compiling wasm-bindgen-externref-xform v0.2.86
       Compiling wasm-bindgen-multi-value-xform v0.2.86
       Compiling threadpool v1.8.1
       Compiling atty v0.2.14
       Compiling socket2 v0.4.9
       Compiling filetime v0.2.21
       Compiling sha1_smol v1.0.0
       Compiling lazy_static v1.4.0
       Compiling termcolor v1.2.0
       Compiling rustc-demangle v0.1.23
       Compiling strsim v0.10.0
       Compiling base64 v0.13.1
       Compiling humantime v2.1.0
       Compiling env_logger v0.8.4
       Compiling rouille v3.6.2
       Compiling wasm-bindgen-cli-support v0.2.86
       Compiling docopt v1.1.1
       Compiling wasm-bindgen-cli v0.2.86
        Finished release [optimized] target(s) in 29.20s
    warning: the following packages contain code that will be rejected by a future version of Rust: buf_redux v0.8.4, multipart v0.18.0
    note: to see what the problems were, use the option `--future-incompat-report`, or run `cargo report future-incompatibilities --id 1`
      Installing /Users/arungupt/Library/Caches/.wasm-pack/.wasm-bindgen-cargo-install-0.2.86/bin/wasm-bindgen
      Installing /Users/arungupt/Library/Caches/.wasm-pack/.wasm-bindgen-cargo-install-0.2.86/bin/wasm-bindgen-test-runner
      Installing /Users/arungupt/Library/Caches/.wasm-pack/.wasm-bindgen-cargo-install-0.2.86/bin/wasm2es6js
       Installed package `wasm-bindgen-cli v0.2.86` (executables `wasm-bindgen`, `wasm-bindgen-test-runner`, `wasm2es6js`)
    warning: be sure to add `/Users/arungupt/Library/Caches/.wasm-pack/.wasm-bindgen-cargo-install-0.2.86/bin` to your PATH to be able to run the installed binaries
    [INFO]: Optimizing wasm binaries with `wasm-opt`...
    [INFO]: ✨   Done in 44.34s
    [INFO]: 📦   Your wasm pkg is ready to publish at /Users/arungupt/projects/hello-wasm/pkg.
    
  • Update Rust code in hello-wasm/src/lib.rs to this:

    use wasm_bindgen::prelude::*;
    
    #[wasm_bindgen]
    extern {
        pub fn alert(s: &str);
    }
    
    #[wasm_bindgen]
    pub fn greet(name: &str) {
        alert(&format!("Hello, {}!", name));
    }
    
  • Update hello-wasm/Cargo.toml to this:

    [package]
    name = "hello-wasm"
    version = "0.1.0"
    authors = ["Your Name <you@example.com>"]
    description = "A sample project with wasm-pack"
    license = "MIT/Apache-2.0"
    repository = "https://github.com/yourgithubusername/hello-wasm"
    edition = "2018"
    
    [lib]
    crate-type = ["cdylib"]
    
    [dependencies]
    wasm-bindgen = "0.2"
    
  • Build the package

    arungupt@Aruns-MBP-52212 hello-wasm % wasm-pack build --target web
    
  • Create hello-wasm/index.html

    <!DOCTYPE html>
    <html lang="en-US">
      <head>
        <meta charset="utf-8" />
        <title>hello-wasm example</title>
      </head>
      <body>
        <script type="module">
          import init, { greet } from "./pkg/hello_wasm.js";
          init().then(() => {
            greet("WebAssembly");
          });
        </script>
      </body>
    </html>
    
  • Start a local web server:

    python3 -m http.server
    
  • Access the webssembly-enabled web application at http://localhost:8000

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment