Jupyter

For data analysis, it's customary and useful to use REPL (interactive language shell) such as Python or R REPLs. Rust has capability for that to using the awkwardly named evcxr crate which can be installed with cargo install --locked evcxr_repl.

Setup of the Rust Jupyter kernel

Potentially more interesting is, however, is the Jupyter notebook Rust kernel which similarly can be installed with: cargo install evcxr_jupyter.

To use Jupyter you can spawn a Jupyter server (running jupyter notebook) or alternatively, use it, directly on VSCode by installing the Jupyter extension, in that case, you can create a new Jupyter file by saving an empty file with the .ipynb extension.

You can then install crates for usage on the notebook by adding the following in Jupyter cell:

:dep polars = {version = "0.32.0", features = ["lazy", "ndarray"]}

You can then use the crates normal fashion in your Rust code.

Hint: saving compilation artifacts across Jupyter kernel sections

When restarting a Jupyter notebook, previously compiled dependencies, will need to be recompiled again, this process can be slow and tedious, to make things faster you can still the sccache, which will save a cache of your compiled crates .

To install it, simply run cargo install sccache --locked. Now to make it work on Jupyter notebooks, all you need to do is add

:sccache 1

to your notebook (before installing the dependencies with :dep).

Note that, if you want to use sccache globally (i.e., not just for Rust on Jupyter but for conventional Rust projects too), you can add the following environment variable (in general by editing your ~/.bashrc file): export RUSTC_WRAPPER=sccache, then you can check if it worked running $RUSTC_WRAPPER on a new terminal. For more details, check the sccache repo.

Hint: updating installed Cargo crates

To update your globally installed Cargo crates installed with cargo install (like evcxr_repl, for instance), you can install the cargo-update crate with cargo install cargo-update and then run cargo install-update -a whenever you want update all your installed crates.


If you found this project helpful, please consider making a donation.