Created
October 12, 2016 20:09
Shared via Rust Playground
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
#[derive(Debug)] | |
struct I(i32); | |
macro_rules! p { | |
($e:expr) => (println!("{:30}: {:?}", stringify!($e), $e)); | |
} | |
trait PrefixStuff { | |
fn strip_prefix<'a>(&'a self, prefix : &Self) -> Option<&'a Self>; | |
} | |
impl PrefixStuff for str { | |
fn strip_prefix<'a>(self : &'a str, prefix : &str) -> Option<&'a str> { | |
if self.starts_with(prefix) { | |
Some(&self[prefix.len()..]) | |
} else { | |
None | |
} | |
} | |
} | |
#[derive(Debug)] | |
enum List<T> {Nil, Cons(T, Box<List<T>>)} | |
impl<T> List<T> { | |
fn unit(x: T) -> List<T> { | |
List::Cons(x, Box::new(List::Nil)) | |
} | |
fn tail(&mut self) -> &mut Self { | |
match *self { | |
List::Nil => unreachable!("Nothing"), | |
List::Cons(_, ref mut t) => {t} | |
} | |
} | |
fn app2(&mut self, val : T) -> &mut Self { | |
let node = List::unit(val); | |
*self = node; | |
self.tail() | |
} | |
fn app2r(s : Ref<Self>, val : T) -> Ref<Self> { | |
Ref(s.0.app2(val)) | |
} | |
} | |
struct Ref<'a, T: 'a>(&'a mut T); | |
/* | |
// Causes timeout | |
impl<'a, T: 'a> From<&'a mut T> for Ref<'a, T> { | |
fn from(p : &'a mut T) -> Self { Ref(p) } | |
}*/ | |
fn liststuff() { | |
let mut lst = List::Nil::<i32>; | |
{ | |
let mut p = Ref(&mut lst); | |
for i in 0..5 { | |
p = List::app2r(p, i); | |
} | |
} | |
p!(lst); | |
} | |
fn main() { | |
MyClass::new(1); | |
let x = 10; | |
let y = vec!(1,5).into_iter().map(|x| I(x)).collect::<Vec<_>>(); | |
p!(y); | |
match x { | |
1 => {println!(1)} | |
_ => println!(2) | |
} | |
liststuff(); | |
//"hello_world"; | |
p!("Hällo World!".to_string().strip_prefix("Hällo ")); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment