Skip to content

Instantly share code, notes, and snippets.

View rust-play's full-sized avatar

The Rust Playground rust-play

View GitHub Profile
@rust-play
rust-play / playground.rs
Created February 9, 2019 20:04
Code shared from the Rust Playground
use std::fmt;
use serde::de::{self, Deserialize, Deserializer, Visitor, SeqAccess, MapAccess};
#[allow(dead_code)]
struct Duration {
secs: u64,
nanos: u32,
}
@rust-play
rust-play / playground.rs
Created February 23, 2019 15:32
Code shared from the Rust Playground
#![allow(unused)]
/*
Kata (6kyu): Scheduling (Shortest Job First or SJF)
Url: https://www.codewars.com/kata/scheduling-shortest-job-first-or-sjf
Scheduling is how the processor decides which jobs(processes) get to use
the processor and for how long. This can cause a lot of problems.
Like a really long process taking the entire CPU and freezing
all the other processes. One solution is Shortest Job First(SJF),
@rust-play
rust-play / playground.rs
Created August 28, 2019 10:55
Code shared from the Rust Playground
use std::time::{ Instant, Duration };
fn benchmark<F: FnOnce()>(f: F) {
let t0 = Instant::now();
f();
let t1 = Instant::now();
let dt = t1 - t0;
println!("{}", dt.as_millis());
}
@rust-play
rust-play / playground.rs
Created August 19, 2019 04:01
Code shared from the Rust Playground
#![forbid(unsafe_code)]
fn main() {
use std::collections::HashMap;
let mut map = HashMap::<Box<dyn DynHash>, _>::new();
fn new<T: DynHash>(value: T) -> Box<dyn DynHash> {
Box::new(value)
}
@rust-play
rust-play / playground.rs
Created December 9, 2019 07:58
Code shared from the Rust Playground
use std::collections::HashMap;
static COLOR_TABLE: [(&str, (u8, u8, u8)); 147] = [
("aliceblue", (240, 248, 255)),
("antiquewhite", (250, 235, 215)),
("aqua", (0, 255, 255)),
("aquamarine", (127, 255, 212)),
("azure", (240, 255, 255)),
("beige", (245, 245, 220)),
("bisque", (255, 228, 196)),
@rust-play
rust-play / playground.rs
Created September 14, 2018 17:36
Code shared from the Rust Playground
extern crate rand; // 0.5.5
use rand::{distributions::Uniform, Rng};
#[derive(Debug, Clone)]
struct Individual {
score: Option<usize>,
value: String,
}
impl Individual {
@rust-play
rust-play / playground.rs
Created July 2, 2019 21:00
Code shared from the Rust Playground
extern crate regex;
use regex::Regex;
fn main() {
let rg = Regex::new(r"(\d)-(\d)(\d)(\d)-(\d)(\d)(\d)(\d)(\d)-(\d)").unwrap();
// Regex::captures returns Option<Captures>,
// first element is the full match and others
// are capture groups
let isbn : Vec<_> = rg.captures("3-598-21508-8").unwrap().iter().skip(1).map(|x| x.unwrap().as_str().parse::<u64>().unwrap()).collect();
println!("{:?}", isbn);
@rust-play
rust-play / playground.rs
Created February 5, 2019 21:55
Code shared from the Rust Playground
extern crate itertools;
extern crate tokio_core;
extern crate web3;
use itertools::Itertools;
use tokio::runtime::Runtime;
use web3::{
futures::{self, Future, Stream},
types::{Block, BlockId, BlockNumber, Transaction},
};
@rust-play
rust-play / playground.rs
Created January 6, 2020 14:42
Code shared from the Rust Playground
#![feature(trait_alias)]
#[derive(Debug, Clone)]
struct Context {
a: A,
b: B,
}
#[derive(Debug, Clone)]
struct A(i32);
#[derive(Debug, Clone)]
@rust-play
rust-play / playground.rs
Created January 22, 2020 14:13
Code shared from the Rust Playground
/*
Exercise about the Rust error handling.
Follow the instructions in the comments "Exercise" below.
References:
Book: https://doc.rust-lang.org/nightly/book/ch09-00-error-handling.html
Rust Standard Library: https://doc.rust-lang.org/stable/std/index.html
Crates IO: https://crates.io/
random: https://rust-random.github.io/rand/rand/fn.random.html