Variables In Rust Programming
In this article, we will learn about the variable declaration in Rust programming. If you have done programming in the past, you will realize that by default variables you declare are mutable i.e. you can change its value anytime. But when we declare a variable in Rust, it is immutable i.e. we are not able to change the value by which it is being initialized.
But it is not the way programming works. We must have the capability to change the initialized value. For this purpose, we use the mut keyword to make a variable mutable. It means we do not create every variable mutable rather we only make certain variables mutable as required.
In this example, tables is immutable and chairs is mutable because we have used the mut keyword in the variable declaration.
One thing also, if we have declared a variable but we do not intend to use it for the time being, just prefix the variable with _(underscore). For example, if the variable is myvar then it will be _myvar.
But why do we need to do it? First, we will suppress the compiler warning and secondly, and any future maintainer of the program can notice which variables are not being used in the program.
Happy reading. Keep in touch.
Comments
Post a Comment