This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fn main() { | |
println!("Hello, world!"); | |
} | |
fn test(holder: Holder) -> impl Animal { | |
match holder { | |
Holder::DogHolder(dog) => dog, | |
Holder::CatHolder(cat) => cat, | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fn main() { | |
println!("Hello, world!"); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
trait Show { | |
fn show(&self) -> String; | |
} | |
impl Show for i32 { | |
fn show(&self) -> String { | |
format!("4 byte signed: {}", self) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type BarberId= usize; | |
type ShopId = usize; | |
struct Shop { | |
barber: Option<BarberId>, | |
} | |
struct Barber { | |
shop: Option<ShopId>, | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { |