Created
April 19, 2018 15:04
-
-
Save b1zzu/30fc3dad3480171ec296a75e757d5432 to your computer and use it in GitHub Desktop.
A simple piece of rust :-)
This file contains hidden or 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 name = String::from("World"); | |
let test = Test::new(&name); | |
test.run().unwrap(); | |
} | |
struct Test<'a> { | |
name: &'a String | |
} | |
impl<'a> Test<'a> { | |
fn new(name: &String) -> Test { | |
Test { name } | |
} | |
fn run(self) -> Result<Test<'a>, String> { | |
ver_fn(Self::ver, box_fn(Self::greet))(self) | |
} | |
fn ver(self) -> Result<Test<'a>, String> { | |
match self.name.as_str() { | |
"World" => Ok(self), | |
_ => Err(String::from("some error")) | |
} | |
} | |
fn greet(self) -> Result<Test<'a>, String> { | |
println!("Hello {}!", self.name); | |
Ok(self) | |
} | |
} | |
fn ver_fn<'a, A: 'a, E: 'a>(method: fn(A) -> Result<A, E>, next: Box<'a + Fn(A) -> Result<A, E>>) -> Box<'a + Fn(A) -> Result<A, E>> { | |
Box::new(move |a: A| -> Result<A, E> { | |
match method(a) { | |
Ok(a) => next(a), | |
Err(e) => Err(e), | |
} | |
}) | |
} | |
fn box_fn<'a, A: 'a, E: 'a>(method: fn(A) -> Result<A, E>) -> Box<'a + Fn(A) -> Result<A, E>> { | |
Box::new(move |a: A| -> Result<A, E> { | |
method(a) | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment