Play with Rust easily

1. Introduction

Unlike intepreted languages, Rust source code needs to be compiled. It often needs depdenencies and this is where Rust’s package manager "cargo" comes into play. This is all working well, however, there are times when you may want to experiment without having to create a new folder, repository, cargo.toml file etc…​

You may be learning and trying many small projects. You may be working on a big project and before you modify it deeply, you may want to experiement a few things on the side. Or you may want to simply use Rust as well in places where its usage may be less obvious, simply because the language is nice, robust and convenient to use.

In this article, we will see how you can run Rust code "on the side" in several convenient ways.

2. Rust Playground

If you are learning Rust, you must have heard of "THE Playground" already. You can find the Rust Playground at https://play.rust-lang.org/. It runs in your browser and you need to install absolutely nothing in order to use it.

The online Rust Playground

The playground actually offers nice features:

  • fairly nice customizable editor (theme, keybindings, etc…​)

  • syntax highlighting

  • shows compilation errors

  • allows sharing your code online (using a gist that will be generated for you on Github)

While the playground shows the output of the compiler, you may at first remain skeptical about some of the issues you run into. This is when the ability to share a code snippet comes nice in to play, allowing others to check out your code and suggest improvements.

The list of your notebooks in Jupyter

You can try for instance opening the code with this link and see if you can fix the (trivial) issue.

3. Jupyter

If you work with people doing research and/or using Python, you likely heard of Jupyter. Jupyter offers an online experience, so once again, you don’t need to install anything locally.

Jupyter allows you to work in so-called Notebooks where you can mix code and markdown. That allows you experimenting and explaining your code while being able to actually execute this code.

The list of your notebooks in Jupyter

Rust shines here because of its raw performance. It comes with a nice touch with you can illustrate your code with nice charts and graphics:

Plotting charts in Rust using Jupyter

Jupyter supports Python as standard kernel but its architecture welcomes additional kernels. The project evcxr allows bringing Rust into Jupyter.

The Rust kernel for Jupyter

Those reading my articles know that I like using Docker. One of the reason is that it helps me keep my machine clean. It is convenient getting a docker image inside which everything works without me having to fiddle, install, update and change anything of the toolchain installed on my local machine.

Unfortunately, I did not find any (working) docker image that provided an off-the-shelf good experience with Jupypter and Rust…​ so I made one.

Given that you have a running Docker installation on your system, you can test the Jupyter-Rust image using:

docker run --rm -it -p 8888:8888 chevdor/jupyter-rust

The following will happen:

 ✘  will@imac  /tmp  docker run --rm -it -p 8888:8888 chevdor/jupyter-rust
Setting theme to monokai
[I 09:12:54.187 NotebookApp] Writing notebook server cookie secret to /home/jupyter/.local/share/jupyter/runtime/notebook_cookie_secret
[I 09:12:55.617 NotebookApp] Serving notebooks from local directory: /notebooks
[I 09:12:55.617 NotebookApp] The Jupyter Notebook is running at:
[I 09:12:55.617 NotebookApp] http://localhost:8888/?token=5f27ea34b2088c0a494799061a25bd254d4cdbb38f9739d8
[I 09:12:55.617 NotebookApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation).
[C 09:12:55.639 NotebookApp]

    To access the notebook, open this file in a browser:
        file:///home/jupyter/.local/share/jupyter/runtime/nbserver-14-open.html
    Or copy and paste one of these URLs:
        http://localhost:8888/?token=5f27ea34b2088c0a494799061a25bd254d4cdbb38f9739d8

You can now grab the link (including the token) and open it in your browser. You will be greeted by a nice and cosy Dark theme:

The list of your notebooks in Jupyter

But fear not if you are on the bright side, there is still an option for your and you can pick among the following themes:

  • chesterish

  • grade3

  • gruvboxd

  • gruvboxl

  • monokai

  • oceans16

  • onedork

  • solarizedd

  • solarizedl

The following theme, for instance, will welcome you with one of those light theme that burns the eyes :)

docker run --rm -it -e THEME=grade3 -p 8888:8888 chevdor/jupyter-rust
The list of your notebooks in Jupyter

You will soon wonder how you can bring on external crates into your notebook and I provided a simple exemple for that:

The list of your notebooks in Jupyter

So if you are more on the academic side of things, you probably already know Jupyter and will likely find that the Rust kernel opens up new possibilities.

4. Cargo Play

The previous options allow you compiling and running Rust code inside your browser (at least apparently…​). The Jupyter Notebook option allows running your code on you machine (although you may also install the rust playground locally). However, there are times where you want to stay within your dev environment and not open "one more tab".

This is where cargo-play becomes handy and especially its vscode extension if you use vscode.

Unlike Jupyter, cargo-play will require you to write a simple main() function such as:

fn main() {
    println!("Say Hi !");
}
The list of your notebooks in Jupyter

5. Wrap up

We have seens 3 options allowing you to experiment with Rust code without having to go through the (relative) burden of scaffholing a cargo project. Two of those options run right off your browser and you can host your own instance at home, in your lab or company. Finally, the last option, while simple, provides a nice local Rust scrach pad keeping all the comfort of your editor.

If you like the content of this article, just bump it up so others can benefit from it as well.

Avatar
Wilfried Kopp aka. Chevdor
Building Blockchains & Decentralized Solutions

I build decentralized solutions and tooling to support them. I am developing Smart Contracts and dApps on Ethereum and developing tooling for Substrate (Polkadot & Kusama). I love Rust! I am using Docker extensively and above all I like efficiency. GPG Fingerprint 15AF C574 D3F9 F1C3 CCDD E31E 2DCE C4DC 506E 6475.

Related