Are there lightweight types in Rust?


Here is a typical programmer’s error:
a numerical argument could stand for a width or a height. Both are integers, but it’s easy to pass invert the arguments (pass them in the wrong order). Then chaos ensues.

To guard against this a notion of a lightweight type would
be like a C typedef, but the compiler would issue a warning or even error if a width were passed to parameter expecting height, unless an explicit cast were done. Multiplying a width by a height yields an ordinary int of course, but this could be converted to an “area” type (for example).

This looks like a zero-cost abstraction to me, and would help protect against programming errors.
My knowledge of Rust is minimal, so I havent had time to check if this exists.

I think Ada has it, but I’m not sure.
yours truly


We call these “newtypes”, and they can be created like so:

  struct Length(u8):
  struct Width(u8);
  let x = Length(1);
  let y = Width(2);
  x = y; // will not compile