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
#![feature(core_intrinsics)] | |
fn print_type_of<T>(_: &T) { | |
println!("{}", unsafe { std::intrinsics::type_name::<T>() }); | |
} |
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
/* Counter example can be seen at https://doc.rust-lang.org/std/iter/index.html#implementing-iterator | |
I just wanna try out implementing Iterator trait on tuple like struct. | |
Due to the IntoIterator blanket implementation in standard library - "impl<I: Iterator> IntoIterator for I" , | |
all Iterator can be treated like IntoIterator. That's why we can call c.into_iter() in the code. | |
*/ | |
use std::iter::*; | |
fn main() { | |
let mut z = 0u32; | |
let ref mut c = Counter(&mut z); |
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
use std::collections::HashMap; | |
fn main() { | |
let mut router = BasicRouter::new(); | |
router.add_route("/", |req| Response { | |
code: 200, | |
headers: req.headers.clone(), | |
body: vec![1, 2, 3, 4, 5], | |
}); |
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
/* | |
To implement a trait on a type, the trait or the type has to be local to the code you work on. It is called the orphan rule. | |
To get around this restriction, we can use the newtype pattern which involves creating a new type in a tuple struct. | |
Both Vec<T> and Display trait are from the standard library and neither is local to our code. | |
We are not able to implement Display trait to Vec<T>. But we can construct a wrapper type holding an instance of Vec<T> and implement Display trait on it. | |
In the following example, VecWrapper<T> struct wraps Vec<T> and implements Display trait. | |
To get all the methods of Vec<T> available to VecWrapper, Deref trait is implemented on it. | |
ref: https://doc.rust-lang.org/book/ch19-03-advanced-traits.html?search=borrow%20and%20asref | |
*/ |
NewerOlder