Rust program to convert an unsigned integer into a signed integer

Rust | convert an unsigned integer into a signed integer: Given an unsigned integer and we have to convert it into the signed integer.
Submitted by Nidhi, on October 08, 2021

Problem Solution:

In this program, we will create an unsigned integer variable then we will assign the value of the unsigned integer variable by converting it into a signed integer variable using the "as" keyword.

Program/Source Code:

The source code to convert an unsigned integer into a signed integer is given below. The given program is compiled and executed successfully.

// Rust program to convert an unsigned 
// integer into a signed integer

fn main() 
{
    let mut UintVar:u8 = 123;
    let mut intVar:i8 = 0;
    
    intVar=UintVar as i8;
    println!("Signed integer is : {}",intVar);
}

Output:

Signed integer is : 123

Explanation:

Here, we created a variable UintVar of the u8 type. Then we assigned the value of the UintVar variable into the i8 variable using the "as" keyword. After that, we printed the result.

Rust Basic Programs »






Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.