Variables and structs and let keywords


#1

image

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?


#2

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.


#3

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.


#4

So struct automatically applies thelet keyword for declaring a variable right?


#5

No; they’re not variables, they’re fields. The most they have in common is that that they’re both storage locations.