3 ms·
The Rust code would be, roughly (I tried to keep names similar even though Rust would use a slightly different naming scheme) let someoneElsesObject = mu.loc
by steveklabnik 6y ago
The Rust code would be, roughly (I tried to keep names similar even though Rust would use a slightly different naming scheme)
let someoneElsesObject = mu.lock();
println!("{}", someoneElsesObject);
return;
when someoneElsesObject goes out of scope, the mutex is unlocked. This happens no matter how it goes out of scope. You cannot forget to do it, because the only way to get access to someoneElsesObject in the first place is locking the mutex, because mutexes wrap the data that they are protecting.
https://crates.io/crates/defer https://crates.io/crates/defer exists, but it would be weird to try and use it here, because it can't really be combined directly with this. I guess in theory you could put drop(someoneElsesObject) in the defer block, but like... that already happens for free.
- knorker 6y agoAh, that looks neat. Among my huge backlog project list, learning Rust is on there. Yeah looks more RAII, and looks like it would not have the problem Go has. Thanks.