Hi Folks, Welcome to another Blog. Today, we'll learn about how to call a rust program using Wasm.
a. For Rust
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
b. For wasm-pack
cargo install wasm-pack
Cargo is a Rust package manager. Cargo downloads your Rust package's dependencies, compiles your packages, makes distributable packages, and uploads them to crates.io, the Rust community's package registry.
cargo new --lib ary-kaush
and open vs code by using
code . command.
we can see that there is a folder named src and a file named cargo.toml.
For adding such specification we need to modify
Cargo.toml file. By adding:
a. crate type:
[lib] crate-type = ["cdylib"]
wasm-bindgen = "0.2.78"
here we are making a function to add two numbers.
after build, a lot of files has been created to support the package.
wasm-pack build --target nodjs
A pkg folder will be created which includes .wasm,.ts, js and .json files.
const math = require('./ary_kaush.js') console.log(math.add2numbers(10,20));
and Finally we have the required output.
This was a basic program in rust, whereas we can create multiple projects e.g. a calculator, fibonacci series, to find whether the number is prime or not etc. .
Stay tuned for more stuff!
Do comment your ideas and suggestions related to the blog and please share if you found it useful.
Write your queries in comment section, we'll help you to resolve your errors.