Closures that Own Their Environment
10 points
Implement make_logger, which returns a closure that formats a message with a fixed prefix, as "{prefix}: {msg}". prefix is a String passed by value into make_logger — the returned closure must capture it by move so it is still valid on every call, long after make_logger itself has returned.
Required signature
pub fn make_logger(prefix: String) -> impl Fn(&str) -> String;
Visible tests
-
formats_with_prefix#[test] fn formats_with_prefix() { let log = make_logger("INFO".to_string()); assert_eq!(log("started"), "INFO: started"); }
Your solution
Log in to submit a solution.