Rust Primitives

In this tutorial you will learn about the Rust Primitives and its application with practical example.

Rust Primitives

A primitive type is a data type is a basic building block provided by any programming language. The primitive data type are predefined by the language and for which the programming language provides built-in support. The Rust provides a range of primitive data types –

Numbers :- The Number data type is used to hold the numeric values. Rust supports following numerical data types –

Integer:- Integers are used to store whole numbers. Rust supports several integer types, varying internal sizes for storing signed and unsigned integers.

Rust Signed Integers
Type Size Description Range
i8 8 bits 8 bit Signed Integer (two’s complement) -128 to 127
i16 16 bits 16 bit Signed Integer (two’s complement) -215 to 215 -1
i32 32 bits 32 bit Signed Integer (two’s complement) -231 to 231 -1
i64 64 bits 64 bit Signed Integer (two’s complement). They can also be represented in octal and hexadecimal -263 to 263 -1
Rust Unsigned Integers
Type Size Description Range
u8 8 bits 8 bit Unsigned Integer 0 to 127
u16 16 bits 16 bit Unsigned Integer 0 to 216 -1
u32 32 bits 32 bit Unsigned Integer 0 to 232 -1
u64 64 bits 64 bit Unsigned Integer 0 to 264 -1

Rust Float:- A Float type is used to store numbers that contain a decimal component (real numbers). Rust provides two floating point types: f32 and f64. The f32 and f64 are used to represent single and double precision numbers.

Rust variable Size Numeric Types :-

Rust Variable Size Numeric Types
Type Signed/Unsigned Description
isize Signed It’s size depends on the size of a pointer of the underlying machine.
usize Unsigned It’s size depends on the size of a pointer of the underlying machine.

Boolean :- The Boolean data type is used to represent the truth values, which can be either True or False. Boolean are commonly used in decision making statements.

Example:-

Output:-

Characters (char) :- The character data type is used to hold the single literal (single Unicode scalar value). Character are declared using single quotes. It can also include emoji or languages character other than English using. Unlike other programming languages Rust’s char is of four byte instead of a single byte.

Example:-

Output:-

String (str) :- A string is a series or sequence of Unicode scalar values encoded as a stream of UTF-8 bytes. In Rust, string handling is bit different from other programming languages. In Rust, we have two types of strings: &str and String. The &str is termed as “string slices”. String slices are basically a reference to a sequence of UTF-8 bytes. A string slice is always of fixed size, and cannot be changed. While, a String series or sequence of characters of – letters, numbers, and special characters. Strings can created by converting from a string slice using the to_string method.

Array :- The array is collection of homogeneous elements placed in contiguous memory locations that can be accessed individually by adding an index or subscript to a unique identifier. By default, arrays are immutable.

Example:-

Output:-

Slice :- A slice is a reference to another data structure. They are useful for same and efficient access to memory location. The slices can be mutable or immutable, and have a defined length.

Tuple :- Tuple is an ordered sequence of comma-separated values (items or elements). Once a tuple is created their elements and size can not be changed.A Tuple is defined within parentheses () where items are separated by commas.

Example:-

In this tutorial we have learn about the Rust Primitives and its application with practical example. I hope you will like this tutorial.