Rust implementations of LeetCode problem #926, compiled to WebAssembly and invoked via Javascript
-
Clone this repository onto your machine.
-
Install
wasm-gcby runningcargo install wasm-gc. This is used bybuild-demo.shto reduce the size of the compiled wasm binary by removing unneccesary/unused cruft. Even though thewasm-gcproject itself says you shouldn't use it in most cases, it still seems to get the wasm file the smallest. I tried using the--gc-sectionsflag in the compiler options andwasm-gcstill got it smaller. I am not making use ofwasm-bindgenorwasm-packin this project, which both attempt to remove cruft when compiling, so I utilizedwasm-gcto do it manually. -
Run
./build-demo.shin the repository root. This creates themonotone_crescendo.wasmbinary and anindex.htmlfile in a directory nameddemoin the repository root. -
cdintodemo/and run an http server. Using python'sSimpleHTTPServer, for example:cd demo/ python -m SimpleHTTPServerThe demo will then be available at
http://localhost:8000.
Detailed documnetation generated via rustdoc can be found alongside the demo.
Huge credit to Dr. Richard Apodaca and his blog, depth-first.com, without which I'm not sure I would have been able to make sense of how to read and write from WebAssembly's linear memory without having to dive straight into something like wasm-bindgen.
These two blog posts from Dr. Apodaca were most helpful:
- Compiling Rust to WebAssembly: A Simple Example
- Rust and WebAssembly from Scratch: Hello World with Strings
Further credit goes to Radu Matei and his blog post, which helped me build upon the concepts I learned from Dr. Apodaca's posts.
The prefix sum solution is the official solution on LeetCode, I only translated it into Rust.
The cumulative solution was posted to LeetCode by tarunbisht, and was translated into Rust by me.