Skip to content

Instantly share code, notes, and snippets.

@rjz
Last active December 24, 2015 04:39
Show Gist options
  • Select an option

  • Save rjz/6745598 to your computer and use it in GitHub Desktop.

Select an option

Save rjz/6745598 to your computer and use it in GitHub Desktop.
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