Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between a type alias and a tuple struct?

Tags:

types

struct

rust

What is the difference between

type CMoves = Vec<Move>;

and

struct CMoves(Vec<Move>);

I understand I can implement traits with the second one, but I cannot with the first one as Vec is defined outside of my crate. Are there any other differences, for example with memory representation?

like image 337
SquattingSlavInTracksuit Avatar asked Dec 18 '17 16:12

SquattingSlavInTracksuit


1 Answers

A type alias is just that, an alias. Pure syntactic sugar with no bearing on semantics. For all intents and purposes you could just replace all of the alias name with what it aliases.

On the other hand, a tuple struct is an entirely separate type, with all that entails:

  • you define its invariants,
  • you define which functions it implements,
  • you define which traits it implements.

As such, the two are nothing alike.

like image 106
Matthieu M. Avatar answered Nov 09 '22 18:11

Matthieu M.