Skip to content

Instantly share code, notes, and snippets.

@mclosson
Created May 23, 2017 07:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mclosson/c41371fd04f4b6ac5e02f9ca99d79076 to your computer and use it in GitHub Desktop.
Save mclosson/c41371fd04f4b6ac5e02f9ca99d79076 to your computer and use it in GitHub Desktop.
fn main() {
fn output<F: Fn(String)>(text: &str, strategy: F) {
let string = String::from(text);
strategy(string)
}
// define strategies as closures
let bold = |s| println!("<strong>{}</strong>", s);
let italics = |s| println!("<em>{}</em>", s);
let header = |s| println!("<h1>{}</h1>", s);
let text = "This is a test.";
// call output using each strategy
output(text, bold);
output(text, italics);
output(text, header);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment