At the end of a struct definition a semicolon is not required. However at the end of a struct tuple it is. Why is that different?
e.g.
fn main() {
struct User {
username: String,
email: String,
}
struct Color(i32, i32, i32);
}
At the end of a struct definition a semicolon is not required. However at the end of a struct tuple it is. Why is that different?
e.g.
fn main() {
struct User {
username: String,
email: String,
}
struct Color(i32, i32, i32);
}
I'm not sure but I guess that this is just a design choice. I'd actually say it's 50/50 on whether we use the semicolon at the end of a type definition or not:
struct MyStruct {}
enum MyEnum {}
struct MyTuple(());
type List = Vec<usize>;
(Note that this excludes the rarely used union
)
The consistency comes from the fact that {}
always makes ending delimiters unnecessary (including semicolons and commas). for example,
if foo {
bar;
}
does not require a semicolon after the }
, even though it's a statement.
likewise in match statements, the arms that have braces don't require trailing commas:
match x {
3 => foo,
4 => { bar }
5 => baz,
}
the { bar }
does not need a comma after it.
Ah, that makes some sense, but I may want to add that this isn't always true:
let foo = match x {
0..20 => {None}
20..40 => {Some(false)}
_ => Some(true)
}; //<-- We need a semicolon here
although in this case it's because we're treating it as an expression, so maybe it doesn't fit your argument...
This. The ;
isn't delimiting the end of the match
, it's delimiting the end of the expression as a whole (which is also the end of the statement).
Also, don't forget that a generic struct
can have a where
clause which comes after the parentheses. The ;
helps tell the parser if it should expect more or not.
Because the where
clause goes in a different place:
fn main() {
struct User where {
username: String,
email: String,
}
struct Color(i32, i32, i32) where;
}