I’ve been making these small Rust riddles for my team at work

137 points yoav_lavi 17 comments

Thought I’d share them here.

if you answer in the comments please use spoiler tags.

Good luck!

Riddle 1:

// the first four words of a popular song

use std::sync::Once;

static ONCE: Once = Once::new();

fn main() {
    let body = Some(Body {});

    if let Some(body) = body {
        ONCE.call_once(|| {
            body.tell(Person::Me);
        });
    }
}

struct Body {}

impl Body {
    fn tell(&self, who: Person) {}
}

enum Person {
    Me,
}

Riddle 2:

// a song name

use std::marker::PhantomData;

enum TreeKind {
    Pvc,
    Pet,
    Abs,
}

struct Song {
    name: Vec<PhantomData<TreeKind>>,
}

Riddle 3:

// a band name

fn disco() {
    let numbers = [1, 2, 3];
    println!("{}", numbers[5]);
}

Riddle 4:

// a song name (with some creative license)

mod man {
    pub struct Zero;
    pub type P1 = Succ<Zero>;
    pub type P2 = Succ<P1>;
    pub type P3 = Succ<P2>;
    pub type P4 = Succ<P3>;
    pub type P5 = Succ<P4>;
}