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 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
@rust-play
rust-play / playground.rs
Created November 26, 2019 06:18
Code shared from the Rust Playground
fn main() {
println!("Hello, world!");
}
fn test(holder: Holder) -> impl Animal {
match holder {
Holder::DogHolder(dog) => dog,
Holder::CatHolder(cat) => cat,
}
}
@rust-play
rust-play / playground.rs
Created January 24, 2020 16:09
Code shared from the Rust Playground
fn main() {
println!("Hello, world!");
}
@rust-play
rust-play / playground.rs
Created January 24, 2020 06:05
Code shared from the Rust Playground
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 December 17, 2019 02:32
Code shared from the Rust Playground
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 December 7, 2018 22:55
Code shared from the Rust Playground
type BarberId= usize;
type ShopId = usize;
struct Shop {
barber: Option<BarberId>,
}
struct Barber {
shop: Option<ShopId>,
}
@rust-play
rust-play / playground.rs
Created June 23, 2018 12:31
Code shared from the Rust Playground
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 August 26, 2019 18:09
Code shared from the Rust Playground
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 March 26, 2018 16:56
Code shared from the Rust Playground
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 March 26, 2018 17:08
Code shared from the Rust Playground
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 {