| 1 |
--- |
| 2 |
kernelspec: |
| 3 |
display_name: Rust |
| 4 |
language: rust |
| 5 |
name: rust |
| 6 |
--- |
| 7 |
|
| 8 |
# Tour of the EvCxR Jupyter Kernel |
| 9 |
For those not already familiar with Jupyter notebook, it lets you write code into "cells" like the box below. Cells can alternatively contain markdown, like this text here. Each code cell is compiled and executed separately, but variables, defined functions etc persist between cells. |
| 10 |
|
| 11 |
## Printing to outputs and evaluating expressions |
| 12 |
Lets print something to stdout and stderr then return a final expression to see how that's presented. Note that stdout and stderr are separate streams, so may not appear in the same order is their respective print statements. |
| 13 |
|
| 14 |
```{code-cell} |
| 15 |
println!("Hello world"); |
| 16 |
eprintln!("Hello error"); |
| 17 |
format!("Hello {}", "world") |
| 18 |
``` |
| 19 |
|
| 20 |
## Assigning and making use of variables |
| 21 |
We define a variable `message`, then in the subsequent cell, modify the string and finally print it out. We could also do all this in the one cell if we wanted. |
| 22 |
|
| 23 |
```{code-cell} |
| 24 |
let mut message = "Hello ".to_owned(); |
| 25 |
``` |
| 26 |
|
| 27 |
```{code-cell} |
| 28 |
message.push_str("world!"); |
| 29 |
``` |
| 30 |
|
| 31 |
```{code-cell} |
| 32 |
message |
| 33 |
``` |
| 34 |
|
| 35 |
## Defining and redefining functions |
| 36 |
Next we'll define a function |
| 37 |
|
| 38 |
```{code-cell} |
| 39 |
pub fn fib(x: i32) -> i32 { |
| 40 |
if x <= 2 {0} else {fib(x - 2) + fib(x - 1)} |
| 41 |
} |
| 42 |
``` |
| 43 |
|
| 44 |
```{code-cell} |
| 45 |
(1..13).map(fib).collect::<Vec<i32>>() |
| 46 |
``` |
| 47 |
|
| 48 |
Hmm, that doesn't look right. Lets redefine the function. In practice, we'd go back and edit the function above and reevaluate it, but here, lets redefine it in a separate cell. |
| 49 |
|
| 50 |
```{code-cell} |
| 51 |
pub fn fib(x: i32) -> i32 { |
| 52 |
if x <= 2 {2} else {fib(x - 2) + fib(x - 1)} |
| 53 |
} |
| 54 |
``` |
| 55 |
|
| 56 |
```{code-cell} |
| 57 |
let values = (1..13).map(fib).collect::<Vec<i32>>(); |
| 58 |
values |
| 59 |
``` |
| 60 |
|
| 61 |
## Spawning a separate thread and communicating with it |
| 62 |
We can spawn a thread to do stuff in the background, then continue executing code in other cells. |
| 63 |
|
| 64 |
```{code-cell} |
| 65 |
use std::sync::{Mutex, Arc}; |
| 66 |
let counter = Arc::new(Mutex::new(0i32)); |
| 67 |
std::thread::spawn({ |
| 68 |
let counter = Arc::clone(&counter); |
| 69 |
move || { |
| 70 |
for i in 1..300 { |
| 71 |
*counter.lock().unwrap() += 1; |
| 72 |
std::thread::sleep(std::time::Duration::from_millis(100)); |
| 73 |
} |
| 74 |
}}); |
| 75 |
``` |
| 76 |
|
| 77 |
```{code-cell} |
| 78 |
*counter.lock().unwrap() |
| 79 |
``` |
| 80 |
|
| 81 |
```{code-cell} |
| 82 |
*counter.lock().unwrap() |
| 83 |
``` |
| 84 |
|
| 85 |
## Loading external crates |
| 86 |
We can load external crates. This one takes a while to compile, but once it's compiled, subsequent cells shouldn't need to recompile it, so it should be much quicker. |
| 87 |
|
| 88 |
```{code-cell} |
| 89 |
|
| 90 |
:dep base64 = "0.10.1" |
| 91 |
base64::encode(&vec![1, 2, 3, 4]) |
| 92 |
``` |
| 93 |
|
| 94 |
## Customizing how types are displayed |
| 95 |
We can also customize how our types are displayed, including presenting them as HTML. Here's an example where we define a custom display function for a type `Matrix`. |
| 96 |
|
| 97 |
```{code-cell} |
| 98 |
use std::fmt::Debug; |
| 99 |
pub struct Matrix<T> {pub values: Vec<T>, pub row_size: usize} |
| 100 |
impl<T: Debug> Matrix<T> { |
| 101 |
pub fn evcxr_display(&self) { |
| 102 |
let mut html = String::new(); |
| 103 |
html.push_str("<table>"); |
| 104 |
for r in 0..(self.values.len() / self.row_size) { |
| 105 |
html.push_str("<tr>"); |
| 106 |
for c in 0..self.row_size { |
| 107 |
html.push_str("<td>"); |
| 108 |
html.push_str(&format!("{:?}", self.values[r * self.row_size + c])); |
| 109 |
html.push_str("</td>"); |
| 110 |
} |
| 111 |
html.push_str("</tr>"); |
| 112 |
} |
| 113 |
html.push_str("</table>"); |
| 114 |
println!("EVCXR_BEGIN_CONTENT text/html\n{}\nEVCXR_END_CONTENT", html); |
| 115 |
} |
| 116 |
} |
| 117 |
``` |
| 118 |
|
| 119 |
```{code-cell} |
| 120 |
let m = Matrix {values: vec![1,2,3,4,5,6,7,8,9], row_size: 3}; |
| 121 |
m |
| 122 |
``` |
| 123 |
|
| 124 |
We can also return images, we just need to base64 encode them. First, we set up code for displaying RGB and grayscale images. |
| 125 |
|
| 126 |
```{code-cell} |
| 127 |
extern crate image; |
| 128 |
extern crate base64; |
| 129 |
pub trait EvcxrResult {fn evcxr_display(&self);} |
| 130 |
impl EvcxrResult for image::RgbImage { |
| 131 |
fn evcxr_display(&self) { |
| 132 |
let mut buffer = Vec::new(); |
| 133 |
image::png::PNGEncoder::new(&mut buffer).encode(&**self, self.width(), self.height(), |
| 134 |
image::ColorType::RGB(8)).unwrap(); |
| 135 |
let img = base64::encode(&buffer); |
| 136 |
println!("EVCXR_BEGIN_CONTENT image/png\n{}\nEVCXR_END_CONTENT", img); |
| 137 |
} |
| 138 |
} |
| 139 |
impl EvcxrResult for image::GrayImage { |
| 140 |
fn evcxr_display(&self) { |
| 141 |
let mut buffer = Vec::new(); |
| 142 |
image::png::PNGEncoder::new(&mut buffer).encode(&**self, self.width(), self.height(), |
| 143 |
image::ColorType::Gray(8)).unwrap(); |
| 144 |
let img = base64::encode(&buffer); |
| 145 |
println!("EVCXR_BEGIN_CONTENT image/png\n{}\nEVCXR_END_CONTENT", img); |
| 146 |
} |
| 147 |
} |
| 148 |
``` |
| 149 |
|
| 150 |
```{code-cell} |
| 151 |
image::ImageBuffer::from_fn(256, 256, |x, y| { |
| 152 |
if (x as i32 - y as i32).abs() < 3 { |
| 153 |
image::Rgb([0, 0, 255]) |
| 154 |
} else { |
| 155 |
image::Rgb([0, 0, 0]) |
| 156 |
} |
| 157 |
}) |
| 158 |
``` |
| 159 |
|
| 160 |
## Display of compilation errors |
| 161 |
Here's how compilation errors are presented. Here we forgot an & and passed a String instead of an &str. |
| 162 |
|
| 163 |
```{code-cell} |
| 164 |
let mut s = String::new(); |
| 165 |
s.push_str(format!("foo {}", 42)); |
| 166 |
``` |
| 167 |
|
| 168 |
## Seeing what variables have been defined |
| 169 |
We can print a table of defined variables and their types with the :vars command. |
| 170 |
|
| 171 |
```{code-cell} |
| 172 |
|
| 173 |
:vars |
| 174 |
``` |
| 175 |
|
| 176 |
Other built-in commands can be found via :help |
| 177 |
|
| 178 |
```{code-cell} |
| 179 |
|
| 180 |
:help |
| 181 |
``` |
| 182 |
|
| 183 |
```{code-cell} |
| 184 |
|
| 185 |
``` |