I am new to Rust and I was reading the Dining Philosophers' tutorial when I found this:
Mutex::new(())
I don't know what the argument inside new
means. I read the documentation for Mutex
and I still have no idea what it means. I would appreciate an explanation about what is happening under the hood.
Python has a lot of built-in functions. The type() function is used to get the type of an object. When a single argument is passed to the type() function, it returns the type of the object.
Function Type Expressions The syntax (a: string) => void means “a function with one parameter, named a , of type string, that doesn't have a return value”. Just like with function declarations, if a parameter type isn't specified, it's implicitly any .
A parameter is a variable in the declaration of the function. An argument is the actual value of the variable that gets passed to the function.
()
is the empty tuple, also called the unit type -- a tuple with no member types. It is also the only valid value of said type. It has a size of zero (note that it is still Sized
, just with a size of 0), making it nonexistent at runtime. This has several useful effects, one of which is being used here.
Here, ()
is used to create a Mutex
with no owned data -- it's just an unlockable and lockable mutex. If we explicitly write out the type inference with the turbofish operator ::<>
, we could also write:
Mutex::<()>::new( () )
That is, we're creating a new
Mutex
that contains a ()
with the initial value ()
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With