Rust Environment

This notebook describes and creates the default Rust environment in Nextjournal. Check out the showcase if you want to see what the environment contains. To see how it’s built, see setup.

Showcase

extern crate image;
extern crate base64;
pub trait EvcxrResult {fn evcxr_display(&self);}
impl EvcxrResult for image::RgbImage {
    fn evcxr_display(&self) {
        let mut buffer = Vec::new();
        image::png::PNGEncoder::new(&mut buffer).encode(&**self, self.width(), self.height(),
            image::ColorType::RGB(8)).unwrap();
        let img = base64::encode(&buffer);
        println!("EVCXR_BEGIN_CONTENT image/png\n{}\nEVCXR_END_CONTENT", img);        
    }
}
impl EvcxrResult for image::GrayImage {
    fn evcxr_display(&self) {
        let mut buffer = Vec::new();
        image::png::PNGEncoder::new(&mut buffer).encode(&**self, self.width(), self.height(),
            image::ColorType::Gray(8)).unwrap();
        let img = base64::encode(&buffer);
        println!("EVCXR_BEGIN_CONTENT image/png\n{}\nEVCXR_END_CONTENT", img);        
    }
}
image::ImageBuffer::from_fn(256, 256, |x, y| {
    if (x as i32 - y as i32).abs() < 3 {
        image::Rgb([0, 0, 255])
    } else {
        image::Rgb([0, 0, 0])
    }
})
215.2s
Rust Tests (Rust)
Rust

Setup

Build the Rust Environment

Download Rust installer.

FILENAME="rust-${RUST_VERSION}-x86_64-unknown-linux-gnu.tar.gz"
FILEURL="https://static.rust-lang.org/dist/${FILENAME}"
wget -qO - https://keybase.io/rust/key.asc | gpg --import -
wget -q --show-progress --progress=bar:force $FILEURL ${FILEURL}.asc
gpg --verify ${FILENAME}.asc $FILENAME
cp $FILENAME /results/
10.8s
Rust Base (Bash)
rust-1.39.0-x86_64-unknown-linux-gnu.tar.gz

Install.

tar -zxf NJ__REF_
cd rust-${RUST_VERSION}-x86_64-unknown-linux-gnu
./install.sh --prefix=/opt/rust
cd ..
rm -r rust-${RUST_VERSION}-x86_64-unknown-linux-gnu
56.3s
Rust Base (Bash)

Install sccache, which caches build results.

cargo install sccache
1197.1s
Rust Base (Bash)
du -hsx /
3.4s
Rust Base (Bash)

Install Packages

Install EvCxR_Jupyter, and some packages.

cargo install evcxr_jupyter evcxr_runtime evcxr_image \
  petgraph-evcxr plotters showata image \
  cargo-edit
1084.7s
Rust (Bash)
Rust Base

Build fake cargo crate to force download/build of dependencies for the cache.

echo '[package]
name = "Nextjournal"
version = "0.1.0"
authors = ["MPD <micah@nextjournal.com>"]
edition = "2018"' > ~/Cargo.toml
0.1s
Rust (Bash)
Rust Base
mkdir -p ~/src
echo 'extern crate evcxr_runtime;
extern crate evcxr_image;
extern crate petgraph_evcxr;
extern crate plotters;
extern crate showata;
extern crate image;
extern crate base64;' > ~/src/lib.rs
0.1s
Rust (Bash)
Rust Base
cd ~/src
for i in "" "--dev" "--build"; do
  cargo add --sort evcxr_runtime evcxr_image \
    petgraph-evcxr plotters showata image base64 $i
done
cargo build
1.1s
Rust (Bash)
Rust Base

Install the Jupyter kernel.

evcxr_jupyter --install
0.8s
Rust (Bash)
Rust Base

Check.

du -hsx /
rustc --version
jupyter kernelspec list
3.5s
Rust (Bash)
Rust Base

Test

println!("Hello world");
eprintln!("Hello error");
format!("Hello {}", "world")
2.1s
Rust Tests (Rust)
Rust
"Hello world"
Runtimes (3)
Runtime Languages (1)