Hello, I'm a new rustacean. My problem is that why the existence of comma in the last line of struct declaration can be compiled successfully.
In Listing 5-1 of rust book, the declaration of struct
struct User {
username: String,
email: String,
sign_in_count: u64,
active: bool,
}
and
struct User {
username: String,
email: String,
sign_in_count: u64,
active: bool
}
Rustc can compile both successfully. Won't that produce some risks?
What risks?
Some reasons for allowing this are
Be lenient with your inputs
diff
s are cleaner (adding a field changes one line, not two)
Making such an addition is simpler / more consistent too
Macros and other code generation are simpler
In the case of tuples, consistency
A 1-tuple requires a trailing comma to distinguish from a parenthesized non-tuple
And presently of course, backwards compatibility
5 Likes
ZiCog
July 30, 2021, 5:02am
3
Which one would you suspect introduces a risk?
Risk of what?
1 Like
system
Closed
October 28, 2021, 5:41am
7
This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.