Skip to content

Instantly share code, notes, and snippets.

@mclosson
Created May 23, 2017 07:34
Show Gist options
  • Save mclosson/c41371fd04f4b6ac5e02f9ca99d79076 to your computer and use it in GitHub Desktop.
Save mclosson/c41371fd04f4b6ac5e02f9ca99d79076 to your computer and use it in GitHub Desktop.

Revisions

  1. mclosson created this gist May 23, 2017.
    20 changes: 20 additions & 0 deletions main.rs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,20 @@
    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);

    }