Victor Ronin
Victor Ronin

Reputation: 23318

struct with parentheses vs double parentheses in Rust

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?

Upvotes: 3

Views: 328

Answers (1)

Peter Hall
Peter Hall

Reputation: 58875

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.

Upvotes: 7

Related Questions