The Rust Programming Language

The Rust Programming Language — Primitives — Numeric Types and Type Annotations

Ankit Tanna
Rustaceans
Published in
3 min readOct 6, 2023

--

We cannot change type of the variable once it is assigned a value of a certain type. Rust compiler will throw an error if you assigned a variable a value of type Number and in subsequent lines, you reassign the variable with a value of String type.

Take for example below code snippet:

fn main() {
let mut x = 1.1;
x = "The quick brown fox...";
let y = 2.2;

println!("x times y is {}!", x*y);
}

Compilation Output:

Once you define a value in rust, that value gets a particular type associated with itself and then the type of the value can’t be changed. You are not allowed to change the type at runtime.

Type Annotations

We can also define the types explicitly in Rust. It is very similar to TypeScript and there are bunch of Type Annotations available in Rust.

fn main() {
let x: f64 =…

--

--