All initialized variables use data-type during declaration to restrict the type of data to be stored. Therefore, we can say that data types are used to tell the variables the type of data they can store.
In this article, we will specifically talk about the Scalar Data types in the Rust programming language. A Scalar type in Rust represents a single value. For example, an int data type only stores single integer data and not multiple integers nor a combination of different data types.
Rust has four primary scalar types:
- Integers
- Floating-point numbers
- Boolean
- Characters
Let's discuss them in detail.
Integer types
Integers are everywhere, Integer is a whole number with a sign and without a fractional part. For example 1, 5, -9, 200001, -15489752are integers
Integers are one of the primitive data types in Rust, Rust compiler can automatically detect Integers and also the programmer can explicitly define integer type.
Example:
Rust
fn main() {
// implicitly defining int
let x = 6;
// explicitly defining unsigned int
let y: u16 = 9;
// explicitly defining signed int
let z: i16 = -7;
println!("{} {} {}", x,y,z);
}
Output:
6 9 -7
In Rust, integer declaration indicates that the value it’s associated with should be an unsigned integer that starts with u (or) signed integer types start with i and takes up 32 bits of space with a minimum of 8 bits.
Length | Signed | Unsigned |
---|
8-bit | i8 | u8 |
16-bit | i16 | u16 |
32-bit | i32 | u32 |
64-bit | i64 | u64 |
128-bit | i128 | u128 |
arch | isize | usize |
The signed complement integers types have:
Type | Minimum | Maximum |
i8 | -(27) | 27 - 1 |
i16 | -(215) | 215 - 1 |
i32 | -(231) | 231 - 1 |
i64 | -(263) | 263 - 1 |
i128 | -(2127) | 2127 - 1 |
Floating-point types
Floating-point numbers integers with the decimal parts. For example 3.22, -5.89, 200.0000. Rust also has a primitive datatype for floating numbers. Rust’s floating-point types are either f32 and f64, which are 32 bits and 64 bits in size, respectively. Like Integers, you can both explicitly and implicitly define floating-point numbers. Syntax of floating-point numbers
Rust
fn main() {
// implicit floating number type
let x = 200.000;
// explicitly floating point type
let y: f64 = 9.894;
let z: f64 = -7.4;
println!("{} {} {}", x,y,z);
}
Output
200 9.894 -7.4
Boolean types
Boolean type is one of the built-in data types provided by Rust, which are defined by the True or False keywords. Generally, it is used to represent the truth values of the expressions. Boolean types in Rust are provided with 1-bit memory. The Boolean type in Rust is specified using the bool keyword.
Rust
fn main() {
// true boolean
let x: bool = true;
// false boolean
let y: bool = false;
println!("{} {}", x,y);
}
Output:
true false
Character types
The character data type is used for storing characters. The keyword used for the character data type is char. Characters typically require 1 byte of memory space and range from -128 to 127 or 0 to 255.
Example:
Rust
fn main() {
let s = 'z';
let c = 'ℤ';
println!("{} {}", s,c);
}
Output
z ℤ
Simple Rust program with all scalar datatypes:
Rust
fn main() {
// Default is "i32"
let g = 9;
println!("{}", g);
// Default is "f64"
let f = 2.9;
println!("{}", f);
// Add explicit type
let e: i64 = 14122020;
println!("{}", e);
// Boolean
let is_active: bool = true;
println!("{}", is_active);
// char
let c = 'a';
println!("{}", c);
let face = '\u{1F600}';
println!("{}", face);
}
Output:
9
2.9
14122020
true
a
????
Similar Reads
Standard I/O in Rust
In this article, we will explore the standard input and standard output in the Rust programming language. Generally, the standard input is provided through the keyboard and the standard output displays values in the console. There are two core traits in Rust, around which the Rust's standard Input
4 min read
Rust - Aliasing
Rust is statically typed and there is a concept of Alias which states that data can be borrowed immutably but during borrowing, the original data cannot be borrowed mutably. Aliasing is useful for the optimization of data. In Aliasing, only one data can be mutably borrowed at a time. On the conditio
2 min read
How to Add a Crate in Rust?
Rust is a highly performant, popular system programming language which means it is mostly used for developing low-level software like operating systems, drivers for hardware, game engines, etc. But you will think that we have already been developing these kinds of software using C/C++ then why a ne
3 min read
Rust - Casting
Type Conversion or casting in Rust is a way to convert one type to another. As its name suggests, type conversion is the process of converting variables from one data type to another. So that, the compiler treats the variable as a new datatype. Rust doesn't allow us to implicitly convert the datatyp
3 min read
Rust - Box<T> Smart Pointer
Box allows us to store data in the heap contrary to the default scheme of Rust to store as a stack. Box is basically used for: For dynamic allocation of memory for variables.When there is a lot of data that we need to transfer ownership and we don't want that they are copied. Let's create a box to s
2 min read
R Data Types
Data types in R define the kind of values that variables can hold. Choosing the right data type helps optimize memory usage and computation. Unlike some languages, R does not require explicit data type declarations while variables can change their type dynamically during execution.R Programming lang
5 min read
Variables in Rust
Variables are memory addresses used to store information to be referenced and manipulated in programs. They also provide a way of defining data with a descriptive name, so our programs can be understood more clearly by the reader and ourselves. It is helpful to think of variables as containers that
4 min read
Rust - The #[derive] Attribute
In Rust, we come across the concept of the #[derive] attribute. Derive attribute is used for generating data structures automatically by using MetalistPaths syntax for specifying the traits that help in implementing paths to derive macros for processing. Expression of the trait is: #[derive(Partial
2 min read
Rust - HashMaps
The concept of HashMap is present in almost all programming languages like Java, C++, Python, it has key-value pairs and through key, we can get values of the map. Keys are unique no duplicates allowed in the key but the value can be duplicated. 1. Insertion In HashMap : To insert a value in a HashM
4 min read
Functions in Rust
Functions are the block of reusable code that can perform similar related actions. You have already seen one of the most important functions in the language: the main function, which is the entry point of many programs. Youâve also seen the "fn" keyword, which allows you to declare new functions. Ru
4 min read