Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does () mean as an argument in a function where a parameter of type T is expected?

Tags:

rust

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.

like image 363
Aleff Avatar asked Feb 22 '16 05:02

Aleff


People also ask

What is the use of type () function in Python?

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.

What does () => void mean TypeScript?

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 .

What is an argument in a parameter?

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.


1 Answers

() 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 ().

like image 96
George Hilliard Avatar answered Oct 26 '22 18:10

George Hilliard