Last active
December 24, 2015 04:39
-
-
Save rjz/6745598 to your computer and use it in GitHub Desktop.
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
| trait Filter { | |
| fn apply(&self, s: &str) -> ~str; | |
| } | |
| struct HelloFilter; | |
| impl Filter for HelloFilter { | |
| fn apply(&self, s: &str) -> ~str { "Hello, " + s } | |
| } | |
| struct EnthusiasmFilter; | |
| impl Filter for EnthusiasmFilter { | |
| fn apply(&self, s: &str) -> ~str { s + "!" } | |
| } | |
| fn main() { | |
| let who = ~"World"; | |
| let filters: &[@Filter] = &[ | |
| @HelloFilter as @Filter, | |
| @EnthusiasmFilter as @Filter | |
| ]; | |
| println(filters.iter().fold(who, |v, f| f.apply(v))); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment