Create a canonical repository of "language quizzes"

This thread was inspired by this tweet from @RReverser:

Expand to view quiz

Quiz: What will this Rust program print?

fn foo(mut f: impl FnMut() + Copy) {
    f();
}

fn bar(mut f: impl FnMut() + Copy) {
    f();
    foo(f);
    f();
    foo(f);
}

fn main() {
    let mut i = 0;
    bar(move || {
        i += 1;
        println!("{}", i);
    });
}

1, 1, 1, 1
1, 2, 3, 4
1, 2, 2, 3
1, 1, 2, 1

I loved the habit of the Go designers publishing such "language quizzes" that highlighted unusual language interactions in a way that teach fundamental parts of the language - in this case, how closures work and are represented. For entertainment purposes, the quizzes frequently feature code that exploits these fundamental properties in terrible ways.

One thing that bothered me was how the community never created any official listing of all the useful quizzes. Twitter does not make digging through archives easy, so it's not an acceptable long term storage form for these instructional artifacts. I'd like to see Rust do better for the newcomers who come in two, four, or more years from now.

Of course, the answer reveal in all of these "terrible exploit" cases should come with a note that the code will generate a warning (:heart: @ekuber)

You have plenty of time to create such quizzes for those newcomers then :slight_smile:

This is why you don't put Copy on your closures.

1 Like