https://github.com/megaconfidence/wasm-fib-calculator
An app that performs computations in WebAssembly written in Rust
https://github.com/megaconfidence/wasm-fib-calculator
rust wasm webassembly
Last synced: about 2 months ago
JSON representation
An app that performs computations in WebAssembly written in Rust
- Host: GitHub
- URL: https://github.com/megaconfidence/wasm-fib-calculator
- Owner: megaconfidence
- Created: 2023-02-21T18:41:35.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2023-07-08T09:13:34.000Z (almost 2 years ago)
- Last Synced: 2025-02-08T22:45:37.524Z (4 months ago)
- Topics: rust, wasm, webassembly
- Language: JavaScript
- Homepage: https://wasm-fib-calculator.netlify.app/
- Size: 10.7 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# wasm-fib-calculator
This project demonstrates how to use WebAssembly in a web app and compares the
performance of JavaScript to Wasm written in Rust## Setup
Create a Rust project. Follow the guide [here](https://doc.rust-lang.org/book/ch01-01-installation.html#installing-rustup-on-linux-or-macos) to install Rust locally
```sh
cargo new wasm-fib-rust
```
Install `wasm-pack`
```sh
curl https://rustwasm.github.io/wasm-pack/installer/init.sh -sSf | sh
```
Add these to the `Cargo.toml` file
```toml
[lib]
crate-type = ["cdylib"][dependencies]
wasm-bindgen = "0.2.84"
```
Rename `main.rs` to `lib.rs` and add the code below
```rs
use wasm_bindgen::prelude::*;#[wasm_bindgen]
pub fn wasmfib(n: i32) -> i32 {
if n < 1 {
return 1;
};
return wasmfib(n - 1) + wasmfib(n - 2);
}
```
Build the project with
```sh
wasm-pack build --target web
```
Clone the starter repo
```
cd ../
git clone https://github.com/megaconfidence/wasm-fib-calculator.git
```
Copy the `pkg/` directory into the project root
```sh
cp -r wasm-fib-rust/pkg wasm-fib-calculator/pkg
```
Startup the project using a static file server
```sh
cd wasm-fib-calculator/
python3 -m http.server 8000
```
Visit the project on [http://localhost:8000/](http://localhost:8000/)