Automatic Type Casting in Rust

Rust

Many modern languages support automatic type casting from one type to another, But Rust does not allow this feature, and any attempt to assign cross types values will result in a compiler error.

let us understand this with below example :

fn main()
{
// This is an attempt to assign a integer to a floating type
let careerAvg:f32 =  40 ;       
}

So if you attempt to assign values like this , Rust compiler will flag a mismatched types error at compile time.

Number Separator

Rust supports the formatting or readability enhancement feature for numbers with huge values. So programmers can format huge numbers like this by using the rust feature:

fn main() 
{
let company_revenue =  20_000_000_000.50_000 ;       //
println!(“Our Company Revenue is {}”, company_revenue);
}

When you run this program you will see below output:

Our Company Revenue is 20000000000.50000

Boolean

Boolean type variables can be assigned true or false values.  The declaration and assignment can be done like this:

fn main() 
{
let isRetired:bool = true;
}

Character

Rust supports character datatype where you can store numbers, alphabets, Unicode characters and also special characters.

The declaration and assignment of a character variables can be done like this:

fn main() 
{
let myNameInitial:char = ‘S’;
let mySymbol = ‘ $‘;
}