Rust Notebook KickStarter
A tutorial of RUST for Jupyter notebooks.
(1..13).map(fib).collect::<Vec<i32>>()
let values = (1..13).map(fib).collect::<Vec<i32>>();
values
use std::sync::{Mutex, Arc};
let counter = Arc::new(Mutex::new(0i32));
std::thread::spawn({
let counter = Arc::clone(&counter);
move || {
for i in 1..300 {
*counter.lock().unwrap() += 1;
std::thread::sleep(std::time::Duration::from_millis(100));
}
}});
*counter.lock()?
*counter.lock()?
*counter.lock()?
:dep base64 = "0.10.1"
base64::encode(&vec![1, 2, 3, 4])
pub fn fib(x: i32) -> i32 {
if x <= 2 {2} else {fib(x - 2) + fib(x - 1)}
}
use std::fmt::Debug;
pub struct Matrix<T> {pub values: Vec<T>, pub row_size: usize}
impl<T: Debug> Matrix<T> {
pub fn evcxr_display(&self) {
let mut html = String::new();
html.push_str("<table>");
for r in 0..(self.values.len() / self.row_size) {
html.push_str("<tr>");
for c in 0..self.row_size {
html.push_str("<td>");
html.push_str(&format!("{:?}", self.values[r * self.row_size + c]));
html.push_str("</td>");
}
html.push_str("</tr>");
}
html.push_str("</table>");
println!("EVCXR_BEGIN_CONTENT text/html\n{}\nEVCXR_END_CONTENT", html);
}
}
let m = Matrix {values: vec![1,2,3,4,5,6,7,8,9], row_size: 3};
m
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])
}
})
:dep tokio = {version = "0.2", features = ["full"]}
let mut stream : tokio::net::TcpStream = tokio::net::TcpStream::connect("127.0.0.1:99999").await?;
let mut stream : tokio::net::TcpStream = tokio::net::TcpStream::connect("127.0.0.1:6573").await?;
use tokio::io::AsyncWriteExt;
stream.write(b"Hello, world!\n").await?;
:vars