So if I am making a struct, then how come I if I typed let red:u8,
then it will result in compile error?
But in other functions, it is the other way round where if I wanted to declare a variable I have to type let red:u8
instead of red:u8
?
So if I am making a struct, then how come I if I typed let red:u8,
then it will result in compile error?
But in other functions, it is the other way round where if I wanted to declare a variable I have to type let red:u8
instead of red:u8
?
Because a struct is not a function, and let
there would be 100% redundant. You also can't define a local variable as pub
.
Also, let
allows patterns and initialisers, struct fields don't.
Here struct
is in the position equivalent to let
:
fn main() {
let A = 1;
struct B {};
}
The first keyword says what kind of language construct it is, and the rest has syntax specific to that construct.
So struct automatically applies thelet
keyword for declaring a variable right?
No; they're not variables, they're fields. The most they have in common is that that they're both storage locations.