Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Rust have an equivalent of C's typedef?

Tags:

C offers the keyword typedef which lets you alias another type:

typedef unsigned int uint; 

This basically makes uint an alias for unsigned int. This also works with more complex types and structures too. Does Rust have a similar language feature? If yes, how are typedefs handled in Rust?

like image 213
typos Avatar asked Dec 18 '17 16:12

typos


People also ask

Does rust have TypeDef?

TypeDef is used to identify and compare types, as well as print their names. If you do not need readable type name, you should use TypeId . This wrapper re-implements TypeId . Since Rust 1.0, this library can only work on nightly Rust.

What is type keyword in Rust?

The type keyword in rust has a different meaning in the 2 places it can be used: Type alias: Just another name for the same type. Associated types: this occurs within traits and trait impl blocks.


1 Answers

Yes. You can simply write

type MyInt = i32; 

These are aliases at the name level, i.e. it is absolutely immaterial which name for the same type you then use. They are perfectly interchangeable.

like image 90
Sebastian Redl Avatar answered Sep 28 '22 09:09

Sebastian Redl