Running a Rust kernel on JupyterLab Desktop


Posted on 2023-12-29, by Racum. Tags: Rust Jupyter

If you want to combine the power of Rust with the free-style research orientation of Jupyter notebooks, try using the Rust kernel on it; this allows for an easy prospective approach, making it very similar to Python.

Install Requirements

Rust

Rust’s documentation recommends installing via rustup:

$ curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

But, if you need an offline installer, or needs a graphical UI, there is also a stand-alone version, but please consider rustup first.

To install the Rust Jupyter kernel, the Rust source is also required, but fortunately this is a one-line operation:

$ rustup component add rust-src

JupyterLab Desktop

JupyterLab and Jupyter Notebooks are both just Python libraries, if you are familiar with Python environments, you could just pip install the package jupyterlab or notebook. But if you are not familiar with environments, you may end up installing it on the “system” Python, and this is not recommended.

If you want your Jupyter flavor to be totally stand-alone, and not even depending on a browser, try JupyterLab Desktop: this is a self-contained conda Python environment, wrapped in a Electron app compatible with Windows, macOS and Linux. Just download the version for your operation system.

...

Install Rust kernel

Thanks to the Evcxr project (EValuation ConteXt for Rust), Rust code can be evaluated on-the-fly, instead of requiring the whole ahead-of-time compilation to produce a binary; making it ideal to power a Jupyter kernel.

First, you need to download and build the kernel itself via cargo:

$ cargo install --locked evcxr_jupyter

Then, use its binary to automatically install it inside Jupyter:

$ evcxr_jupyter --install

After installed, you’ll see the Rust logo on the Jupyter launcher; your setup is ready! 🎉

...

The Rust kernel behaves more-or-less like the Python default one: create your functions and call them to see the evaluated results.

...

Performance notes

  • Every time you run a cell from the Rust kernel, a new context is created and evaluated, making Rust seen slower than is actually is.
  • The evaluation context doesn’t compile with optimizations, this is a research and discovery tool, if you want your code to actually run at full speed, copy it into a proper Rust project and compile it with cargo build --release.

Bonus: Rust REPL

If you are coming from a language that provides a REPL (read–eval–print loop) in the terminal, the Evcxr project can also help you here, just install it via cargo as well (also requires rust-src):

$ cargo install --locked evcxr_repl

It looks like this:

$ evcxr
Welcome to evcxr. For help, type :help
>> fn double(x: isize) -> isize {x * 2}
>> double(10)
20