Skip to content

Instantly share code, notes, and snippets.

Avatar

The Rust Playground rust-play

View GitHub Profile
@rust-play
rust-play / playground.rs
Created Jan 24, 2020
Code shared from the Rust Playground
View playground.rs
trait Show {
fn show(&self) -> String;
}
impl Show for i32 {
fn show(&self) -> String {
format!("4 byte signed: {}", self)
}
}
@rust-play
rust-play / playground.rs
Created Dec 17, 2019
Code shared from the Rust Playground
View playground.rs
pub struct SqsConsumerRouter<S>
where S: Sqs + Clone + Send + 'static
{
receiver: tokio::sync::mpsc::Receiver<SqsConsumerMessage>,
actor_impl: SqsConsumer<S>,
}
impl<S> Future for SqsConsumerRouter<S>
@rust-play
rust-play / playground.rs
Created Jan 24, 2020
Code shared from the Rust Playground
View playground.rs
fn mock<T: Into<String>, I: IntoIterator<Item = T>>(_: I) {}
struct Merchant<'a> {
name: &'a str,
billing_portal: &'a str,
billing_period: &'a str,
stripe_id: Option<&'a str>,
}
fn merchant() -> Merchant { unimplemented!(); }
@rust-play
rust-play / playground.rs
Created Dec 7, 2018
Code shared from the Rust Playground
View playground.rs
type BarberId= usize;
type ShopId = usize;
struct Shop {
barber: Option<BarberId>,
}
struct Barber {
shop: Option<ShopId>,
}
@rust-play
rust-play / playground.rs
Created Feb 23, 2019
Code shared from the Rust Playground
View playground.rs
#![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 Jun 23, 2018
Code shared from the Rust Playground
View playground.rs
fn main() {
let numbers = vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10].into_iter();
let evens = numbers.filter(|x| *x % 2 == 0);
let even_squares = evens.clone().map(|x| x * x);
let result = even_squares.clone().collect::<Vec<_>>();
println!("{:?}", result); // prints [4,16,36,64,100]
println!("{:?}\n{:?}", evens, even_squares);
}
@rust-play
rust-play / playground.rs
Created Jan 24, 2020
Code shared from the Rust Playground
View playground.rs
fn main() {
println!("Hello, world!");
}
@rust-play
rust-play / playground.rs
Created Aug 26, 2019
Code shared from the Rust Playground
View playground.rs
pub trait HKT<'r, B> {
type A; // Current type
type MA; // Type A swapped with B
type MB; // Current Container type
}
#[macro_export]
macro_rules! derive_hkt {
($t:ident) => {
impl<'r, B: 'r, C> HKT<'r, C> for $t<B> {
@rust-play
rust-play / playground.rs
Created Mar 26, 2018
Code shared from the Rust Playground
View playground.rs
fn main() {
println!("{:?}", series("92017", 0));
}
fn series(digits: &str, len: usize) -> Vec<String> {
let mut result: Vec<String> = Vec::new();
if len <= digits.len() && len != 0 {
result = digits
.as_bytes()
.windows(len)
@rust-play
rust-play / playground.rs
Created Mar 26, 2018
Code shared from the Rust Playground
View playground.rs
fn collatz(mut n: u64) -> Option<u64> {
let mut step = 0;
if n == 0 {
return None;
}
while n != 1 {
if n % 2 == 0 {
n /= 2;
} else {