The Rust Programming Language — Primitives — Floats and Immutability
In this article, we are going to learn about float primitive type.
Floats are a primitive type in Rust which allow you to deal with numbers and decimals. Take for example below code snippet:
fn main() {
let x = 1.1;
let y = 2.2;
println!("x times y is {}!", x*y);
}The code snippet looks fairly similar to the ones that we have used in string interpolation examples, with an additional steps of generating the result of x times y and also coverting it to the string to substitute the {} for generating the final string output.
Typically, if we go by Maths, we should expect the output of x*y to be 2.42 but the output we get in the console is 2.4200000000000004. This is typically because all programming languages perform mathematical operations for decimals using binary system which is quite faster. Modern hardware, for decimal math, has a lot of overhead and multiple instructions which could take significant hit in performance.
There is also one more thing to note here is that println!() is not actually a function call. It is called a macro in Rust programming language. Any call that ends with ! is a macro. A macro is nothing but a line of code which subsequently get’s expanded into set of instructions which are required to perform that macro. So, in our above code snippet, for the line:
println!("x times y is {}!", x*y);the println!() would get expanded into:
- Evaluation of
x - Evaluation of
y - Evaluation of
x*y - Substitution of
{}with result ofx*y - Printing of the statement on to the console
You can define your own macro. The difference between macro and a function would be the !.
Immutability
In Rust, the variables defined using let keyword are immutable. Hence, when you define a variable x and try to reassign it, the compiler throws an error.
Take example of below snippet:
fn main() {
let x = 1.1;
x = 4.0;
let y = 2.2;
println!("x times y is {}!", x*y);
}
To get more details about the error, you can execute the command rustc --explain E0384

You can define mutable variables by adding mut in front of the variable name and after the let keyword. This is slightly a different syntax as compared to JavaScript or other programming languages.
fn main() {
let mut x = 1.1;
x = 2.2;
// x = 2.2;
let y = 2.2;
println!("x times y is {}!", x*y);
}Compiling this code, Rust compiler does warn us of unwanted values assigned to x.

Immutability in Rust can be achieved using let keyword and that is equivalent to JavaScript’s Object.freeze() method which prevents the object’s properties and their values to be changed.
To summarise immutability in Rust: let means the value is constant and immutable, let mut means the value is neither constant nor immutable.
Rust likes to have it’s variables immutable by default and hence there is a need of an extra keyword mut to be added before the variable name. In practice, it is way too common to use only let as compared to let mut.
So this was Floats primitive type and immutability concept in Rust.
Please share your feedback in the comments.
You can subscribe to my newsletter about The Rust Programming Language here. You can read about all the articles in this series here.
Rustaceans 🚀
Thank you for being a part of the Rustaceans community! Before you go:
- Show your appreciation with a clap and follow the publication
- Discover how you can contribute your own insights to Rustaceans.
- Connect with us: X | Weekly Rust Newsletter






