Unlock the Power of Rust in Electron Apps
Electron has revolutionized the world of cross-platform desktop applications, powering popular platforms like Atom, VS Code, Spotify, Slack, and Discord. However, JavaScript’s slow performance can hinder the user experience. That’s where Rust comes in – a low-level language with high-level ergonomics, boasting blazing fast speed and a tiny memory footprint.
Setting Up an Electron Project
To get started, we’ll use electron-react-boilerplate to create a boilerplate for our demo application. Simply clone the GitHub repo, install dependencies using npm or Yarn, and you’re ready to build a note-taking app with markdown support.
Adding UI to Your Electron Project
Material-UI provides ready-made components for our application. We’ll implement a parse function to convert markdown to HTML, exploring various methods to interface between Electron and Rust.
The Power of WebAssembly in Electron
At its core, an Electron app is just a Chrome browser and Node.js. By leveraging WebAssembly support in Chrome and Node, we can compile our markdown parser written in Rust into WebAssembly.
Creating a Rust Markdown Parser in WebAssembly
Rust has first-class support for WebAssembly, using LLVM to compile for WebAssembly as a target. The wasm-pack
tool helps developers write Node modules in Rust using WebAssembly. We’ll create a boilerplate for the WebAssembly module using wasm-pack new
.
Using wasm-pack
wasm-pack
provides an installer script for Linux and macOS. Once installed, we can create a boilerplate for the WebAssembly module. The lib.rs
file is the main file for the Wasm library, exporting the greet
function called by JavaScript.
A Close Look at lib.rs
wasm-bindgen
adds boilerplate code to our functions, allowing us to access strings on the JavaScript heap. We’ll use the comrak
crate, which supports GitHub-flavored markdown, to create a parse function that takes a markdown string as an argument and returns the HTML string.
Using WebAssembly in Electron
We can use our Rust markdown parser in both browser-side and Node.js APIs in Electron. With React’s useEffect
, we can optimize our application’s performance.
Using NAPI in Electron
NAPI is a C header that communicates with Node.js. We’ll use node-bindgen
to create a Rust library that can be used in Node.js directly. This method is useful when the application requires a thread, SIMD, or processor-specific code.
Other Libraries
There are additional libraries available, such as Neon, napi-rs, and nodejs-sys, which use V8 APIs or NAPIs.
Comparison and Choosing the Right Approach
Both methods have pros and cons, and the right choice depends on the use case and requirements. Use the chart below to help you select the most appropriate approach.
Unlocking Full Visibility into Web Frontends for Rust Apps
Debugging Rust applications can be challenging, especially when users experience issues that are hard to reproduce. LogRocket provides full visibility into web frontends for Rust apps, automatically surfacing errors, and tracking slow network requests and load time. Try LogRocket today and modernize how you debug your Rust apps!