DEV Community

DIPANKAR PAUL
DIPANKAR PAUL

Posted on

Type Casting in Rust

Type casting is a fundamental concept in programming that allows us to convert variables from one data type to another. In Rust, type casting is done using the as keyword. However, Rust's type system is strict, and not all types can be cast to each other. In this article, we'll explore how type casting works in Rust and some of its limitations.

Floating Point to Integer

You can convert a floating-point number to an integer in Rust using the as keyword:

fn main() {
    let decimal: f32 = 64.31;
    let integer = decimal as u16;

    println!("decimal = {}", decimal); // 64.31
    println!("integer = {}", integer); // 64
}
Enter fullscreen mode Exit fullscreen mode

We are converting data from one type to another type manually using the as keyword. This type of type casting is also known as Explicit Type Casting.

Character to Integer

Characters in Rust are internally represented as Unicode Scalar Values, which are essentially numeric representations of characters. You can convert a character to an integer like this:

fn main() {
    let character: char = 'A';
    let integer = character as u8;

    println!("character = {}", character); // A
    println!("integer = {}", integer); // 65
}
Enter fullscreen mode Exit fullscreen mode

Integer to Character

You can also convert an integer to a character in Rust:

fn main() {
    let integer: u8 = 65;
    let character = integer as char;

    println!("integer = {}", integer); // 65
    println!("character = {}", character); // A
}
Enter fullscreen mode Exit fullscreen mode

Error while converting integer to character

We are only allowed to use u8 integers while performing type casting between integer and character. If we use any other integer type and convert it to a character, we will get an error. For example,

Example:

fn main() {
    let integer: i32 = 65;

    let character = integer as char;  //! invalid cast
    // only `u8` can be cast as `char`, not `i32`

    println!("integer = {}" , integer);
    println!("character = {}", character);
}
Enter fullscreen mode Exit fullscreen mode

Here, we have used i32 data type instead of u8. Hence we get an error. It's because Unicode Scalar Values are small integer numbers and fit in the range of u8 data type.

Boolean to Integer

Booleans can be converted to integers, where false becomes 0 and true becomes 1:

fn main() {
    let boolean1: bool = false;
    let boolean2: bool = true;

    let integer1 = boolean1 as i32;
    let integer2 = boolean2 as i32;

    println!("boolean1 = {}", boolean1); // false
    println!("boolean2 = {}", boolean2); // true
    println!("integer1 = {}", integer1); // 0
    println!("integer2 = {}", integer2); // 1
}
Enter fullscreen mode Exit fullscreen mode

Limitations of Type Casting

Rust's type system is strict, and not all types can be cast to each other. For example, you cannot directly convert a floating-point number to a character:

fn main() {
    let decimal: f32 = 65.321;
    let character = decimal as char; // Error: only `u8` can be cast as `char`, not `f32`

    println!("decimal = {}", decimal);
    println!("character = {}", character);
}
Enter fullscreen mode Exit fullscreen mode

Conclusion

Understanding type casting is essential for working effectively with different data types in Rust. While Rust's type system is strict, it ensures safety and prevents common programming errors. By using type casting correctly, you can convert variables between different types when needed, ensuring your Rust programs are both efficient and reliable.

Top comments (0)