In a language with nullable pointers or references you might write: (using C syntax for example’s sake)
void my_func(Foo* foo) {
assert(foo);
do_stuff_with(*foo);
}
If, in Rust, we phrase that as:
fn my_func(foo: Option<&Foo>) {
let foo = foo.assert();
do_stuff_with(*foo);
}
you can think of this as assert() returning evidence of the truth of the assertion (if it’s true; otherwise of course it won’t return). I think this fits in very nicely with the broader philosophy of “helping the compiler help you” by exposing your reasoning to it with types and evidence.
In the C example, the only connection between assert(foo) and do_stuff_with(*foo) is in the programmer’s head. In the Rust example, the compiler knows about it, and will let you know if you get it wrong.
(So in case it’s not obvious: I’m taking the perspective that assert() is not a new name for unwrap(), but is actually an assert in the traditional sense, which has been improved to return useful evidence.)