3 ms·
What's going on here is a bit of shorthand. If you don't use the shorthand, the signature looks like: fn access<'a, T: Send>(guard: &'a mut MutexGuard<T>)
by aturon 12y ago
What's going on here is a bit of shorthand. If you don't use the shorthand, the signature looks like:
fn access<'a, T: Send>(guard: &'a mut MutexGuard<T>) -> &'a mut T;
This says that the incoming borrow and the returned borrows have identical scopes (officially called "lifetimes" in Rust; 'a here is a lifetime variable.) It's a very typical pattern: if you return some borrowed data, it generally comes from a borrow you were given, and the shorthand allows you to leave this implicit in cases where it's clear.
EDIT FOR CLARITY: in particular, you're saying that if you take a "sublease" from something you've already borrowed, that sublease is valid for no longer than the original lease. So for example if you borrow a field out of a borrowed structure, the access to the field can last no longer than you had access to the original structure. In this case, we're saying that access to the data lasts no longer than the lock is held.
I'd recommend http://blog.skylight.io/rust-means-never-having-to-close-a-socket/ http://blog.skylight.io/rust-means-never-having-to-close-a-s... if you want to get a bit more of a feel for how this works.
The details about the shorthand are here: https://github.com/rust-lang/rfcs/pull/141 https://github.com/rust-lang/rfcs/pull/141