Interesting, great example. This kind of compile time safety can't be achieved without borrow checker, I guess? I'm now thinking hard if there's some type magic similar to the exhaustive checks for Typescript discriminated unions ("kind satisfies never" checks in switch statements).
I've been trying to come up with a similar pattern in TS many times but I think you cannot do this due to the lack of moves (bindings do alias, pass by reference).
But TS has so many dark magical patterns that I'm still hoping to be proven wrong.
A simple example of the (many) things you can't do in TS:
Try doing that without a runtime crash in TS. Super useful pattern for state machines (which are the building block of computing).I miss more features of Rust in TS that features of TS in Rust. All the time.