Applications Google
Menu principal

Post a Comment On: Coder's Log

"Rust Memory Management"

6 Comments -

1 – 6 of 6
Blogger Adrien Plazas said...

Thanks, this will be useful to me. :)

May 8, 2017 at 1:47 AM

Anonymous Paolo Bonzini said...

That's really clear, thanks. Just a question: when you wrote "let mut status = self.status.lock().unwrap();", I thought the mutex would get unlocked immediately after "unwrap()" finishes, because "lock()" returns a MutexGuard and the only reference to the MutexGuard is unreachable after "unwrap()" finishes.

Why am I wrong? And where is "unwrap()" defined on MutexGuard?

May 8, 2017 at 9:55 AM

Anonymous Anonymous said...

Is that a typo at the end of the "Mutex" section: "When the unlocked value goes out of the scope, mutex is automatically unlocked."

The first unlocked should probably read locked, or am I missing something?

May 9, 2017 at 9:40 AM

Blogger zeenix said...

Paolo, Mutex::lock() returns a LockResult> rather than the MutexGuard and unwrap() is a method of Result (LockResult is just a specific type of Result). Related docs:

https://doc.rust-lang.org/std/sync/struct.Mutex.html#method.lock
https://doc.rust-lang.org/std/sync/type.LockResult.html
https://doc.rust-lang.org/std/result/enum.Result.html#method.unwrap

May 9, 2017 at 2:43 PM

Blogger zeenix said...

Anonymous, thanks! Yes that's a typo.

May 9, 2017 at 2:43 PM

Anonymous Paolo Bonzini said...

Oh, I was missing the automatic deref (MutexGuard implements Deref so it can be treated as a String). The syntax/semantics of automatic deref is still a bit mysterious to me...

May 9, 2017 at 10:29 PM

Please no insults to anyone
You can use some HTML tags, such as <b>, <i>, <a>

Comment moderation has been enabled. All comments must be approved by the blog author.

You will be asked to sign in after submitting your comment.
Please prove you're not a robot