Showing posts with label Rust. Show all posts
Showing posts with label Rust. Show all posts

Thursday, December 25, 2025

Rust and WebAssembly quick start guide 2025

Rust and WebAssembly quick start guide 2025

As of December 2025, Rust and WebAssembly is reorganizing. Documentation is currently out of date and not working. This is my notes on getting started using Rust WebAssembly. Most of the documentation still works - below is distilled version.

Install Rust toolchain
$ curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
Below shows my Rust toolchain versions
q@x1m9:~/tmp$ cargo --version
cargo 1.92.0 (344c4567c 2025-10-21)
q@x1m9:~/tmp$ rustc --version
rustc 1.92.0 (ded5c06cf 2025-12-08)


Install wasm-pack - your one-stop shop for building, testing, and publishing Rust-generated WebAssembly.
$ sudo apt install build-essential
$ cargo install wasm-pack
wasm-pack version
q@x1m9:~/tmp$ wasm-pack --version
wasm-pack 0.13.1


Install cargo-generate - helps you get up and running quickly with a new Rust project by leveraging a pre-existing git repository as a template.
$ sudo apt install libssl-dev
$ sudo apt install pkg-config
$ cargo install cargo-generate
cargo-generate version
q@x1m9:~/tmp$ cargo-generate --version
cargo generate 0.23.7


Install npm

Install node version manager - nvm
$ curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.3/install.sh | bash
nvm version
q@x1m9:~/tmp$ nvm --version
0.40.3


Now install latest version of node
$ nvm install node
node version
q@x1m9:~/tmp$ node --version
v25.2.1


Clone project template
$ cargo generate --git https://github.com/rustwasm/wasm-pack-template.git --name wasm-game-of-life
$ cd wasm-game-of-life


Build the project
$ wasm-pack build


Putting it into a webpage, run in project root
$ sudo apt install git
$ npm init wasm-app www


Install dependencies
$ cd ~/p/wasm-game-of-life/www
$ npm install


Edit ~/p/wasm-game-of-life/www/package.json
$ nvim ~/p/wasm-game-of-life/www/package.json 


Add wasm-game-of-life to dependencies section of package.json and also note that devDependencies is updated.
{
  "name": "create-wasm-app",
  "version": "0.1.0",
  "description": "create an app to consume rust-generated wasm packages",
  "main": "index.js",
  "bin": {
    "create-wasm-app": ".bin/create-wasm-app.js"
  },
  "scripts": {
    "build": "webpack --config webpack.config.js",
    "start": "webpack-dev-server"
  },
  "repository": {
    "type": "git",
    "url": "git+https://github.com/rustwasm/create-wasm-app.git"
  },
  "keywords": [
    "webassembly",
    "wasm",
    "rust",
    "webpack"
  ],
  "author": "Ashley Williams ",
  "license": "(MIT OR Apache-2.0)",
  "bugs": {
    "url": "https://github.com/rustwasm/create-wasm-app/issues"
  },
  "homepage": "https://github.com/rustwasm/create-wasm-app#readme",
  "devDependencies": {
    "webpack": "^5.0.0",
    "webpack-cli": "^5.0.0",
    "webpack-dev-server": "^4.0.0",
    "copy-webpack-plugin": "^11.0.0",
    "hello-wasm-pack": "^0.1.0"
  },
  "dependencies": {
    "wasm-game-of-life": "file:../pkg"
  }
}


Edit ~/p/wasm-game-of-life/www/index.js
$ nvim ~/p/wasm-game-of-life/www/index.js


Add wasm-game-of-life package to index.js
import * as wasm from "wasm-game-of-life";

wasm.greet();


Install wasm-game-of-life package and force clean as we have upgraded devDependencies
$ cd ~/p/wasm-game-of-life/www
$ rm -rf node_modules package-lock.json
$ npm install


Fix ~/p/wasm-game-of-life/www/webpack.config.js
$ nvim ~/p/wasm-game-of-life/www/webpack.config.js


Observe we have changed CopyWebpackPlugin and added asyncWebAssembly
const CopyWebpackPlugin = require("copy-webpack-plugin");
const path = require('path');

module.exports = {
  entry: "./bootstrap.js",
  output: {
    path: path.resolve(__dirname, "dist"),
    filename: "bootstrap.js",
  },
  mode: "development",
  plugins: [
    new CopyWebpackPlugin({
      patterns: [
        { from: "index.html" },
      ],
    })
  ],
  experiments: {
    asyncWebAssembly: true,
  },
};


Now run it
$ npm run start


Open http://localhost:8080

Friday, April 18, 2025

Rust on QNX

Rust on QNX

This blog shows how to build a Rust toolchain for QNX 7.1 OS.

Below is my environment:
Host OS: Linux Mint 22.1
QNX: 7.1

To get started, let's get lastest Rust so we can use it to bootstrap the new toolchain
$ curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
Install development dependencies
$ sudo apt install build-essential cmake python3 perl git llvm-dev
Setup
$ source ~/qnx710/qnxsdp-env.sh
$ mkdir ~/repo
$ cd ~/repo
$ git clone https://github.com/rust-lang/rust.git
$ cd rust
$ #create config.toml
$ echo 'profile = "compiler"' > config.toml
$ echo change-id = 999999 >> config.toml
Update environment variables for CC_/CF_FLAGS/CXX_/AR_, this is targetting x8_64
$ export build_env='
    CC_x86_64_unknown_nto_qnx710=qcc
    CFLAGS_x86_64_unknown_nto_qnx710=-Vgcc_ntox86_64_cxx
    CXX_x86_64_unknown_nto_qnx710=qcc
    AR_x86_64_unknown_nto_qnx710=ntox86_64-ar
    '
Build the toolchain
$ env $build_env \
    ./x.py build \
        --target x86_64-unknown-linux-gnu,x86_64-pc-nto-qnx710 \
        rustc library/core library/alloc library/std
Now, let's tell Rust that there is a toolchain we call qnx-toolchain
$ rustup toolchain link qnx-toolchain ~/repo/rust/build/x86_64-unknown-linux-gnu/stage1/
And for convenience, make it the default:
$ rustup default qnx-toolchain
There should be a Rust binary located like below:
$ ls ~/prj/hello-world/target/x86_64-pc-nto-qnx710/debug/hello-word
Now copy this file into QNX 7.1 OS (x86_64) target and run the hello-world

Sunday, January 07, 2024

Rust: Using Windows thread pool

Rust: Using Windows thread pool

Quick demo how to use Windows thread pool in Rust using windows crate.

Create new project

PS C:\> mkdir prj
PS C:\> cd prj
PS C:\> cargo new thread_pool_work

Edit Cargo.toml like below

Update src/main.rs

Run thread_pool_work

PS C:\prj\thread_pool_work> cargo run
   Compiling thread_pool_work v0.1.0 (C:\prj\thread_pool_work)
    Finished dev [unoptimized + debuginfo] target(s) in 0.64s
     Running `target\debug\thread_pool_work.exe`
counter: 10
Tags: Rust, Windows, thread pool

CPP Quick Guide

Basics Hello world User input While loop If statement For loop Switch statement Read file using ifstream Write to a file using ofstr...