Wasm Builders 🧱

Cover image for Rust With WebAssembly
Kirtee Prajapati
Kirtee Prajapati

Posted on

Rust With WebAssembly

WebAssembly is, supported by 40 high-level programming languages including C and C++, Python, Go, Rust, Java, and PHP.
But Rust with wasm amalgamation is considered the deadly one.
You can check out his Blog: WebAssembly x Rust, a Deadly Combination! by @aryank21 for interesting facts about rust.

So let's move towards the coding part before starting you have to install the following:

Environment setup

1. Install Rust

$ curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
Enter fullscreen mode Exit fullscreen mode

For more details or manual download visit the official site.

2. Install Wasmtime

Runtime for webAssembly and WASI

$ curl https://wasmtime.dev/install.sh -sSf | bash
Enter fullscreen mode Exit fullscreen mode

3. Install the WebAssembly Rust toolchain:

 rustup target install wasm32-wasi
Enter fullscreen mode Exit fullscreen mode

4. Code Snippets

Create a new project with cargo new <project_name> and switch to the hello-rust directory using the command below:

# Here my file name is hello-rust

cargo new hello-rust
cd hello-rust
Enter fullscreen mode Exit fullscreen mode

Inside main.rs file paste the bellow code:

// Program to print Hello, world!
fn main() {
    println!("Hello, Rust!");
}

Enter fullscreen mode Exit fullscreen mode

5. Compiling Rust Code

  1. Compile using cargo
cargo build
cargo run
Enter fullscreen mode Exit fullscreen mode
  1. Compile to wasm
cargo build --target=wasm32-was --release
Enter fullscreen mode Exit fullscreen mode
  1. Wasm runtime

wasmtime target/wasm32-wasi/release/hello-rust.wasm
Enter fullscreen mode Exit fullscreen mode

Rust Screenshot3

Image description

So it was that easy isn't it in just 5 simple steps!!

References:

  1. https://www.rust-lang.org/what/wasm
  2. https://enarx.dev/docs/WebAssembly/Rust
  3. https://medium.com/@knoldus/rust-with-webassembly-simple-explanation-with-a-nice-example-6959c0bbeafc

Oldest comments (0)