skip to content
Ratul's Blog

Interactive Rust with jupyter

// Adding a Rust kernel to jupyter notebooks.



My experience with notebooks

I havenā€™t always been the biggest fan of jupyter notebooks, but over the past year or so Iā€™ve really grown to love using them. There are two scenarios in particular in which I find the notebook experience to be greatly superior to traditional methods of coding.

The first is when any sort of tabular data is involved. Figuring out how to manipulate a Pandas dataframe in a terminal, whilst having to repeatedly run python main.py, just isnā€™t fun.

The second, and probably what Iā€™m most looking forward to doing now with Rust, is when you want to learn or teach someone else how to code. When it comes to interactive coding, the notebook experience is unmatched, giving you the ability to play around, experiment and iterate on a few lines of code at a time.

Iā€™ve recently learnt that I can add a variety of kernels (you can think of these as programming languages) other than Python to the classic jupyter notebook experience and so thatā€™s what Iā€™m going to show you how to do here. Iā€™m currently trying to teach myself Rust, so this post will be about adding a Rust kernel, but the same general approach will likely apply to any other kernel you want to add.

Adding a Rust kernel to jupyter

Iā€™m going to go ahead and assume you already have jupyter notebooks up and running, but if you donā€™t then you can install it with pip install notebook.

Step 1: Install Rust

From what I can tell, the most recommended approach to installing Rust is to use rustup. This will install rustc, cargo, rustup and other standard Rust tools to the relevant .cargo directory on your device.

You can find the guide for installing Rust using rustup linked here.

Once complete, verify your Rust installation by running the following:

rustc --version

Step 2: Installing the Rust jupyter kernel

Once you have Rust installed, we can move on to installing the Rust kernel for jupyter.

You can find a comprehensive list of all available jupyter kernels in the jupyter github repo. The one we are interested in, is called the EvCxR Jupyter Kernel.

EvCxR: An evaluation context for Rust.

You can either download the prebuilt binary from their releases page or build it from source by running:

cargo install evcxr_jupyter

Itā€™s worth noting that this step may take a few minutes.

Step 3: Add the Rust kernel to jupyter

Once installed, we need to let jupyter know that we have a new kernel to use. We can do this by running the following:

evcxr_jupyter --install

Step 4: Create a new notebook using the Rust kernel

Run jupyter notebook to open up the classic notebook experience and create a new notebook. If all went well, you will hopefully notice that you a new kernel available when creating a notebook. Select the one called Rust.

And thatā€™s it. You can now write some Rust code in a jupyter notebook!

Iā€™m really looking forward to seeing if this helps make learning Rust a tiny bit easier. If you decide to try this out as well, definitely toot at me on mastodon - will be keen to hear your thoughts on this.

Thanks for reading! šŸ«¶

I would love to hear your thoughts on this. If you have any questions or comments, please feel free to reach out on any of the platforms below. I look forward to connecting with you!