struct with parentheses vs double parentheses in Rust

Viewed 110

What is the difference between?

struct Test();

struct Test(());

I understand that a struct can have tuples (unnamed fields). However, I am not sure what does (()) mean in such a case? That it's a struct with one element which is an empty tuple?

1 Answers

it's a struct with one element which is an empty tuple?

That's exactly right.

Technically these are different types, but they carry exactly the same amount of information as each other; none.

There is really no purpose in having that argument, and you generally wouldn't create a type like struct Test(()) except in a generic context. For example,

struct Test<T>(T);

where T ends up being () due to some other requirements.

Related