Remix.run Logo
▲ lollollol3 a day ago

What is funny is that Rust has similar or worse issues regarding temporary lifetimes. Only some UB is caught (and in practice only sometimes), issues like deadlocks aren't.

https://fasterthanli.me/articles/a-rust-match-made-in-hell

Challenge to you, simonask: Explain why the curly braces here determines whether there is a deadlock or not. Bonus points if you do so without asking for help or looking anything up.

https://play.rust-lang.org/?version=stable&mode=debug&editio...

https://play.rust-lang.org/?version=stable&mode=debug&editio...

▲simonask a day ago | parent [-]

I'm responding so future readers see the answer, because oooh boy am I not engaging with this account.

The curly braces shortens the lifespan of the `MutexGuard` so it gets unlocked before the body of the if-statement. Normally, the temporary used in the if-let pattern lives until the end of the if-statement.

This wouldn't compile if the value guarded by the Mutex wasn't trivially copyable, but it is.

(Note how none of this is UB, just a particularly contrived way to introduce a deadlock.)

▲ 8 hours ago | parent [-]
[deleted]