3 ms·
Python's with keyword maybe? You can do with lock: # lock is held # lock released with lock: # lock is held again Also Go's defer k
by remram 4y ago
Python's with keyword maybe?
You can do
with lock:
# lock is held
# lock released
with lock:
# lock is held again
Also Go's defer keyword, which is bound to the current function (defer until function returns).
- jolux 4y agoRight, but those are opt-in features. In Python you can lock something and not release it by mistake. In Rust this pattern is enforced, the destructor will always release it at the end of the enclosing scope unless there is a panic or infinite loop. I think this is a substantive difference.
- tazjin 4y agoIt's not enforced in Rust, but it's automatic unless you actively prevent it from happening. So you could say it's an opt-out feature. std::mem::forget is considered safe, as explained in its docs: https://doc.rust-lang.org/std/mem/fn.forget.html https://doc.rust-lang.org/std/mem/fn.forget.html
- jolux 4y agoFair enough, yeah.