Prerequisites
Install Rust following the install guide
Evcxr
Rust programs can also be executed in an interactive environment. Evcxr is an evaluation context for Rust and it provides a REPL (analog to ipython), and a Jupyter Kernel.
Installing the Jupyter Kernel
The steps to install the jupyter kernel are the following:
- Install the
evcxr_jupyterbinary:cargo install --locked evcxr_jupyter - Install the kernel:
evcxr_jupyter --install(Note: ensure that$HOME/.cargo/binis in yourPATHvariable)
Using the Jupyter Kernel in Visual Studio Code
- Install the
jupyterpackage in your python environment - Install the Jupyter extension for VS Code
- Create a new
.ipynbfile and open it - Select the Jupyter Kernel
- Click on the Select Kernel button in the top right
- Choose Jupyter Kernel …
- Choose the Rust evcxr kernel you installed earlier
Importing dependencies
Now that you have selected the kernel you can start writing code and executing it. To import dependencies you can use the :dep keyword1.
:dep renoir = { git = "https://github.com/deib-polimi/renoir" }Now with an use statement we can import what we need to use renoir.
use renoir::prelude::*;Recommended prelude
The evcxr kernel can be tuned using special keywords according to your needs. We list some of the most useful tweaks you can make (these can be put in a cell at the beginning of the notebook)
:cache SIZESet cache size in MiB (use for faster compilation):opt LEVELSet the optimization level, default is no optimization (for faster execution use 1,2 or 3)
Example
:cache 500
:opt 1
:dep renoir = { git = "https://github.com/deib-polimi/renoir" }
use renoir::prelude::*;let ctx = StreamContext::new_local();
let result = ctx.stream_par_iter(0..100)
.map(|x| (x, x * x))
.collect_vec();
ctx.execute_blocking();
let output = result.get()// The :vars keyword will print the variables you have set (Note: Rust lifetime rules still apply!)
:vars| Variable | Type |
| output | Option<Vec<(i32,i32)>> |
println!("{output:?}");Some([(0, 0), (1, 1), (2, 4), (3, 9), (4, 16), (5, 25), (6, 36), (7, 49), (8, …
Footnotes
it follows the same syntax of cargo toml ↩