What is const _: () = {} and should you use it?

109 points lashyn_mk 23 comments

I've come across some Rust code that includes a snippet that looks like the following (simplified):

const _: () = {
    // ...
    // test MIN
    assert!(unwrap!(I24Repr::try_from_i32(I24Repr::MIN)).to_i32() == I24Repr::MIN);
}

I suppose it can be seen as a test that runs during compile time, but is there any benefit in doing it this way? Is this recommended at all?

Source: https://github.com/jmg049/i24/blob/main/src/repr.rs