Primitive types

See also: List of primitive types in the standard library documentation

Primitive types are types that are built into the language. Most other types are composed of these primitive types. Primitive types have special syntax – for example, numbers can be created with number literals.

Numeric types
Numeric types are integers (whole numbers) and floating-point numbers. Unsigned integers can never be negative. Signed integers can be negative and are represented as Two's complement.

The floating-point numbers implement the binary32 and binary64 formats specified in IEEE 754. This means that they are represented as a sign bit, an exponent and a fraction. They can be NaN ("not a number") and (–)Infinity. Because NaN is specified to not be equal to itself, floating-point numbers implement the trait, but not.

Number literals
There are two types of number literals,  and  ; the compiler tries to infer one of the integer types for   literals and  or  for   literals. If the type can't be inferred,  defaults to  and   defaults to. It is possible to explicitly specify the type with a suffix, e.g.,.

A number literal can contain any number of underscores, as long as it doesn't start with an underscore, and the decimal point isn't immediately followed by an underscore.

Syntactically, the sign is not part of the literal. However, Rust checks if the literal is preceded by a sign, so compiles, even though  does not.

literals must contain a decimal point (e.g. or ) or an exponent (e.g. ( or ), or must have a  or  suffix (e.g. ).

literals can be specified in different formats. The default format is decimal. Hexadecimal literals start with, octal literals start with  , binary literals with. The prefix can't contain underscores.

Number literals with a type suffix can be immediately followed by a dot and a method call. For example, and  work.

For exists a special kind of literal, the byte literal, which is a  literal prefixed with , e.g. . The character must be in the range. Even though it looks like a character, its type is.

A value that can be or. is represented as, is represented as. This type is used for boolean logic. For example and  expressions as well as the ,   and   operators operate on  values.

Truth tables
Note: The red values are not evaluated because of short-circuiting.

is a character of text, or more precisely, a Unicode scalar value. This is similar to, but not the same as a Unicode code point, because a may never contain a high-surrogate or low-surrogate code point. has a size of 4 bytes.

Note that some s aren't visible characters, and what looks like a single character, can consist of multiple s.

literals
literals are s surrounded by single quotes. They may contain the following escape sequences:


 * Quotes:  and
 * ASCII escapes:
 * (null, U+0000)
 * (horizontal tab, U+0009)
 * (line feed, U+000A)
 * (carriage return, U+000D)
 * (backslash, U+005C)
 * followed by an octal digit and a hexadecimal digit, e.g
 * Unicode escapes:  ...  . Between the braces go 1 to 6 hexadecimal digits. Underscore are allowed as well, e.g.

A chunk of UTF-8 encoded text. Because the text can have any length, this is a dynamically-sized type.

is not the same as. While a is always 4 bytes wide, characters in a  have a variable width between 1 and 4 bytes. Lower code points, like ASCII, have a more compact representation than higher code points, such as Chinese characters or emojis.

Because of this, s in a string can't be indexed in constant time. Furthermore, the method returns the length in bytes, not in characters.

String literals
String literals have the type. They have the lifetime, because they are included in the   section of the executable, so they are always alive.

String literals are written in double quotes. They can have the same escape sequences as char literals. Furthermore, a backslash at the end of the line can be used to remove the line break as well as all subsequent whitespace characters:

Raw string literals
In raw string literals, escape sequences are ignored. Raw string literals are prefixed with an, and can be enclosed with an arbitrary number of number signs. The number of number signs before and after the string must be equal:

Function pointer ()
A type that points to a function:

Unlike closures, function pointers can't access their surrounding context, so it's often better to use or a generic  instead of.

Tuple (, )
Main article: Tuple

A tuple is a sequence of elements with a certain length. The elements can have different types:

The unit is the empty tuple. It is the type of statements and the implied type of functions that don't specify a return type. Since it has only one possible value, it is zero-sized:

,
Main article: Arrays and slices

Arrays and slices  are consecutive sequences of values of the same type. While arrays have a fixed size known at compile time, slices are dynamically sized and can only be used behind a reference or pointer-like type:

Pointer, reference
See also: Reference

Never
The type, also called uninhabited type, indicated with an exclamation point. The never type can never exist at runtime, because no value of this type exists. It is the return type of divergent functions, which never return:

Using  in other places than the return type of a function, is experimental and requires the   nightly feature. A workaround is to use an enum with no variants:

The standard library contains a similar type,, which is used as the error type for conversions that never fail. For example:

When the  feature is stabilized, it will be possible to replace Infallible with   everywhere. Note that this is a zero-cost abstraction thanks to Rusts layout optimizations.

is a bottom type, which means that it is a subtype of every other type, and can be coerced to any type. This is exploited when using or another panicking macro, which evaluates to  :

has no impact on type inference. When no type is explicitly specified, Rust infers the unit type, which becomes apparent in some error messages:

implements many traits from the standard library, to make it easier to use it in generic contexts.