Quantcast
Channel: Hacker News
Viewing all articles
Browse latest Browse all 737

The Periodic Table of Rust Types

$
0
0
The Periodic Table of Rust Types
Immutable ReferenceMutable ReferenceOwnedBare
Raw*T
Immutable raw pointer
*mutT
Mutable raw pointer
Raw pointers do not have ownership.
Simple&'rT
Immutable borrowed pointer
&'rmutT
Mutable borrowed pointer
~T
Owned pointer
T
Primitive type, struct, enum and so on
Vector&'r [T]
Immutable borrowed vector slice
&'rmut [T]
Mutable borrowed vector slice
~[T]
Owned vector
[T]
Unsized vector type
String&'r str
Immutable borrowed string slice
&'rmut str
Mutable borrowed string slice
~str
Owned string
str
Unsized string type
Trait&'rTrait:K
Immutable borrowed trait object
&'rmutTrait:K
Mutable borrowed trait object
~Trait:K
Owned trait object
Trait
Unsized trait type
Call-many(no shorthand)
&'r Fn<T…,U>:K
Closure with immutable environment
'r |T…|:K-> U
&'rmut Fn<T…,U>:K
Closure with mutable environment
(no shorthand)
~Fn<T…,U>:K
Closure with owned environment
extern"ABI"fn(T…) -> U
Bare function type
Call-once(no shorthand)
&'r OnceFn<T…,U>:K
Procedure with immutable environment
(no shorthand)
&'rmut OnceFn<T…,U>:K
Procedure with mutable environment
proc(T…) -> U
~OnceFn<T…,U>:K
Procedure with owned environment
Bare functions always can be called multiple times.
Supported as of 0.9
Proposed (DST and Variadic generics)
Impossible
'rLifetime
:KTrait bounds
T…Function arguments
-> UFunction return
extern"ABI"ABI definition

What is this?

This "periodic table" is a cheatsheet for various Rust types. Rust programming language has a versatile type system that avoids a certain class of memory error in the safe context, and consequently has somewhat many types at the first glance. This table organizes them into an orthogonal tabular form, making them easier to understand and reason. I also hope that this makes very obvious why Rust needs such seemingly many types.

The periodic table was made by Kang Seonghoon as a thought experiment, and then... it have got redditted unexpectedly :p Henceforth I've made the URL permanent and added some descriptions.

Discussion: /r/rust, /r/programming, Hacker News

Explanation

Rows indicate the ownership. There are two big groups from left to right: indirect and direct (i.e. referenced or owned). Indirect types can be coerced horizontally: ~T can be coerced to &mutT or &T and &mutT can be coerced to &T. Direct types should be explicitly converted (via &, &mut or ~ expression) to indirect types. (Exception: a bare function type is in the bare group but can be coerced to the closure.)

Columns indicate the different kind of types. There are three big groups from top to bottom: unsafe deference, safe deference and callable.

Colored backgrounds (sorry the accessibility!) indicate the current availability. Black background means the type is plain absurd and prohibited. Gray background means the type makes some sense but it is not yet in the language. Fortunately we have two proposals that cover all the missing types now:

  • Dynamically sized types proposal (a.k.a. DST) brings unsized types to the language. Normally most types including references and owned pointers have their size known and thus are "sized", but [T], str and Trait do not have their size known in the compile time. The proposal allows them in the limited context, and that is primarily useful for fully supporting custom smart pointers. This also has a side effect that allows for &mut str, though it won't see much use since safe strings cannot be modified via indexing.
  • Variadic generics proposal brings a variadic number of generic parameters, similar to that of C++11. Consequently it can turn closures and procedures into simple traits. The exact interface is not yet settled (one of the possible interface discussed is indicated below in the small print) so it can look differently in the future.

There are some optional syntactic parts possible in types. Many of them are turned off by default since they are normally verbose, but you can turn them on if you want.


a part of cosmic.mearie.org.
The Periodic Table of Rust Types
Immutable ReferenceMutable ReferenceOwnedBare
Raw*T
Immutable raw pointer
*mutT
Mutable raw pointer
Raw pointers do not have ownership.
Simple&'rT
Immutable borrowed pointer
&'rmutT
Mutable borrowed pointer
~T
Owned pointer
T
Primitive type, struct, enum and so on
Vector&'r [T]
Immutable borrowed vector slice
&'rmut [T]
Mutable borrowed vector slice
~[T]
Owned vector
[T]
Unsized vector type
String&'r str
Immutable borrowed string slice
&'rmut str
Mutable borrowed string slice
~str
Owned string
str
Unsized string type
Trait&'rTrait:K
Immutable borrowed trait object
&'rmutTrait:K
Mutable borrowed trait object
~Trait:K
Owned trait object
Trait
Unsized trait type
Call-many(no shorthand)
&'r Fn<T…,U>:K
Closure with immutable environment
'r |T…|:K-> U
&'rmut Fn<T…,U>:K
Closure with mutable environment
(no shorthand)
~Fn<T…,U>:K
Closure with owned environment
extern"ABI"fn(T…) -> U
Bare function type
Call-once(no shorthand)
&'r OnceFn<T…,U>:K
Procedure with immutable environment
(no shorthand)
&'rmut OnceFn<T…,U>:K
Procedure with mutable environment
proc(T…) -> U
~OnceFn<T…,U>:K
Procedure with owned environment
Bare functions always can be called multiple times.
Supported as of 0.9
Proposed (DST and Variadic generics)
Impossible
'rLifetime
:KTrait bounds
T…Function arguments
-> UFunction return
extern"ABI"ABI definition

What is this?

This "periodic table" is a cheatsheet for various Rust types. Rust programming language has a versatile type system that avoids a certain class of memory error in the safe context, and consequently has somewhat many types at the first glance. This table organizes them into an orthogonal tabular form, making them easier to understand and reason. I also hope that this makes very obvious why Rust needs such seemingly many types.

The periodic table was made by Kang Seonghoon as a thought experiment, and then... it have got redditted unexpectedly :p Henceforth I've made the URL permanent and added some descriptions.

Discussion: /r/rust, /r/programming, Hacker News

Explanation

Rows indicate the ownership. There are two big groups from left to right: indirect and direct (i.e. referenced or owned). Indirect types can be coerced horizontally: ~T can be coerced to &mutT or &T and &mutT can be coerced to &T. Direct types should be explicitly converted (via &, &mut or ~ expression) to indirect types. (Exception: a bare function type is in the bare group but can be coerced to the closure.)

Columns indicate the different kind of types. There are three big groups from top to bottom: unsafe deference, safe deference and callable.

Colored backgrounds (sorry the accessibility!) indicate the current availability. Black background means the type is plain absurd and prohibited. Gray background means the type makes some sense but it is not yet in the language. Fortunately we have two proposals that cover all the missing types now:

  • Dynamically sized types proposal (a.k.a. DST) brings unsized types to the language. Normally most types including references and owned pointers have their size known and thus are "sized", but [T], str and Trait do not have their size known in the compile time. The proposal allows them in the limited context, and that is primarily useful for fully supporting custom smart pointers. This also has a side effect that allows for &mut str, though it won't see much use since safe strings cannot be modified via indexing.
  • Variadic generics proposal brings a variadic number of generic parameters, similar to that of C++11. Consequently it can turn closures and procedures into simple traits. The exact interface is not yet settled (one of the possible interface discussed is indicated below in the small print) so it can look differently in the future.

There are some optional syntactic parts possible in types. Many of them are turned off by default since they are normally verbose, but you can turn them on if you want.


a part of cosmic.mearie.org.

Viewing all articles
Browse latest Browse all 737

Trending Articles