`with` clauses

If I understand correctly, this is an incredibly powerful idea:

fn main() {
    let logger = EnvLogger::new();
    with(logger: &logger) {
        run();
    }
}

fn run() {
    // uses implict logger!
    log::info("starting to do thing...");
    do_thing();
    log::info("done thing");
}

fn do_thing() {
    // ...
    log::info("...");
    // ...
}

#[test]
fn test_run() {
    let logger = TestLogger::new();
    with(logger: &logger) {
        run();
    }
}

crate log {
    fn info(s: &str) with(logger: &dyn Logger) {
        logger.info(s);
    }
}

But also comes with drawbacks that have been discussed over the years:

2 Likes