I was interested in Rust, and so I started reading the Rust programming guide on the Rust website, and discovered that variables are declared with the following syntax:
let x: i32 = 5;
Which means assign the value integer 5 to the variable type integer 32 bit which shall be referred to by the notation x from this point onwards.
Why is the let keyword reequired? It seems redundant, what does it actually "do"?
I assume the compiler would be able to tell that the following is a variable (or const variable) declaration:
x: i32 = 5;
There doesn't appear to be a reason for a let keyword, but presumably there is a smart reason because Rust is focused on safety. So what is that reason?
Edit: Addition: As function arguments, the let keyword is not required. Here is an example:
fn add1(x: i32) -> i32
{
x = x + 1
}
This seems a bit strange - this "looks like" a pass by reference because of the missing let. But it's not. It's a pass by value. (Or at least I think it is.) Is this a syntactic inconsistency?
As an aside, why is the ordering of the variable name and type different from other languages? For example, the syntax could have been:
i32 x = 5;
Put a colon in there if you prefer.
i32: x = 5;
- When programming you usually know what "type" of data you want before you think of a name for it. Maybe that's just something that comes from a C++ background, perhaps some think the other way around?
This brings me to another point; how can one declare a several variables of the same type in Rust? Such as:
let x, y, z: i32 = {4, 5, 5} // A guess of what this might look like?
Or is this just not allowed in Rust?
&or other indicator, is beyond me.let, what does that have to do with by-value/by-reference?letit missing makes it look (to me personally) like it's not the creation of a new variable. Therefore perhaps it is something else. Perhaps that something else is a reference? What else could it be?