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 datatype between primitive types.
The primitive types are i8, i16, i32, i64, u8, u16, u32, u64, f32, f64, char, and bool. Here i8, i16, i32, i64 are signed integer types and u8, u16, u32, u64 are unsigned integer types. f32 and f64 are floating-point types. char is a single character type and bool is a boolean type.
The term implicitly means that the compiler does not require us to write the code to convert the data type. The compiler does this automatically. But rust doesn't allow us to implicitly convert the datatype between primitive types. We have to explicitly convert the data type using the as keyword.
Type of casting:
- Implicit type casting
- Explicit type casting
1. Implicit Type Casting:
Implicit typecasting is the process of converting one data type to another. The compiler does this automatically.
Rust
// Implicit type casting results in an error
fn main() { // main function
// declaring the variable with float datatype
let x = 1000.456456154651;
// converting the float datatype to integer datatype
// results in an error
let y: i32 = x;
println!("The value of y is: {}", y);
}
Â
Â
Output:
Â
Rust doesn't allow implicit type conversion
Â
Rust compiler can not simply convert the float datatype to integer data type, we have to explicitly convert the data type. Therefore, if we try to run the above code, we will get an error.Â
Â
2. Explicit Type Casting:
Â
Explicit typecasting can be done by using the as keyword. The as keyword is used to safely convert the datatype. The compiler checks if the datatype is correct or not. If the datatype is correct, then the compiler converts the datatype. If the data type is incorrect, then the compiler gives an error. To do a safe conversion, typecasting should be done from low datatype to high data type.
Â
Example 1:
Â
Rust
// Rust program to convert the datatype from i32 to u32
// function to print the type of a variable
fn print_type_of<T>(_: &T) {
println!("{}", std::any::type_name::<T>())
}
fn main() { // main function
// declare x as i32
let x: i32 = 100000;
// explicitly convert the datatype from i32 to u32
let y: u32 = x as u32;
// print the type of x
println!("Before Type Conversion of x ");
print_type_of(&x);
// print the type of y
println!("After Type Conversion of x ");
print_type_of(&y);
}
Â
Â
Output:
Â
type conversion from i32 to u32
Â
Example 2:
Â
Rust
// Rust program to convert the datatype from i32 to u32
// function to print the type of a variable
fn print_type_of<T>(_: &T) {
println!("{}", std::any::type_name::<T>())
}
// type casting
fn main() {
// declare x as i32
let decimal_value = 65.4321_f32;
// converting to integer and truncating
let integer_value = decimal_value as u8;
// converting integer to character using ascii table
let character_value = integer_value as char;
// printing the values and their datatype
println!("The value of decimal_value is: {} and its datatype is ", decimal_value);
print_type_of(&decimal_value);
println!("The value of integer_value is: {} and its datatype is ", integer_value);
print_type_of(&integer_value);
println!("The value of character_value is: {} and its datatype is", character_value);
print_type_of(&character_value);
// printing the type of the variables
println!("Casting from {} -> {} -> {}", decimal_value, integer_value, character_value);
}
Â
Â
Output:
Â
Type conversion
Â
Similar Reads
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
Rust - Constants Constants are the value that can not be changed after assigning them. If we created a constant, then there is no way of changing its value. The keyword for declaring constant is const. In Rust, constants must be explicitly typed. Â The below syntax is used to initialize a constant value: Syntax : con
1 min read
Rust - Creating a Library Rust is a multi-paradigm programming language like C++ syntax that was designed for performance and safety, especially for safe concurrency. Also, it is a compiled system programming language. In this article, we will see how to create libraries in Rust. Creating a Rust Library:Step 1: We start by c
1 min read
Rust Use Declaration In Rust, we have a concept of the 'use' keyword. This keyword is used for importing or at times renaming items from other different modules or crates. Use the keyword to find its usage when we are required to import items or rename items from crates or modules in Rust programs. For precisely calling
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