Earlier quoted context omitted.
As others have said the principal is: no semicolons after things that use {}, semicolons after everything else.
This isn't quite true. Semi-colons are required after all expressions in rust (even when braces are present). e.g. `let x = match y { ... };`
Rust Language Cheat Sheet
21–30 of 65 posts
Re: Rust Language Cheat Sheet
#22As a Rust noob, what's the rhyme or reason for semicolons? I see the example: struct S {} but here the semicolon is required: type T = S; or you get weird errors. It's hard to predict when a semicolon is required at least for type declarations, is there an underlying principle?
Re: Rust Language Cheat Sheet
#23Ralf Biedert, thanks so much for this! I've been learning Rust via the Rust Book for the past few months and this will help so, so much - fantastic work.
I know everyone says how good Rust's documentation is but I tried learning Rust with The Rust Book and found it frustrating: too long and wordy on the easy bits and without useful insight on the hard bits. I ended up buying Programming Rust by Jim Blandy [0] on a recommendation from HN comments and found it must easier to learn from. I'm already familiar with several other languages, which that book assumes, so your…
Re: Rust Language Cheat Sheet
#24As a Rust noob, what's the rhyme or reason for semicolons? I see the example: struct S {} but here the semicolon is required: type T = S; or you get weird errors. It's hard to predict when a semicolon is required at least for type declarations, is there an underlying principle?
Optional after expressions.
Re: Rust Language Cheat Sheet
#25Earlier quoted context omitted.
As others have said the principal is: no semicolons after things that use {}, semicolons after everything else.
This isn't quite true. Semi-colons are required after all expressions in rust (even when braces are present). e.g. `let x = match y { ... };`
Re: Rust Language Cheat Sheet
#26Earlier quoted context omitted.
Scheme has to fall somewhere between Smalltalk and Forth for quantity of syntax too. To me, these are the prettiest languages.
I didn't mention lisps because I'm never sure how to classify "built-in" special forms: should they be considered normal names and thus omitted from a syntactic overview or should they be considered keywords and included? On the one hand they usually have non-normal evaluation and custom mini-languages, on the other so do most macros otherwise you'd just use a function. But yeah, these are the sort of languages I thi…
Re: Rust Language Cheat Sheet
#27Earlier quoted context omitted.
I didn't mention lisps because I'm never sure how to classify "built-in" special forms: should they be considered normal names and thus omitted from a syntactic overview or should they be considered keywords and included? On the one hand they usually have non-normal evaluation and custom mini-languages, on the other so do most macros otherwise you'd just use a function. But yeah, these are the sort of languages I thi…
Yes, and I'm not sure the R*RS standards even specify which special forms are fundamental vs implemented by macros. However, Smalltalk has some keywords too. In any case, it could be a concise cheat sheet.
Indeed but they're all on the postcard above (true, false, nil, super and self).
Smalltalk's closest thing to built-in special forms would be primitives but they have a generic "shape" and they're pretty much always used as implementation details for the "normal" messages which system users interact with.
Re: Rust Language Cheat Sheet
#28Re: Rust Language Cheat Sheet
#29Earlier quoted context omitted.
This isn't quite true. Semi-colons are required after all expressions in rust (even when braces are present). e.g. `let x = match y { ... };`
Technically here the `;` is for the `let`, which is a statement, not for the match expression within it. The braces are part of the match, not of the let. So the let doesn't end with a block, and thus requires a semicolon.
Yet everyone here is making a distinction between blocks and not-blocks. Is "my rule" better?
Furthermore, if you say the semicolon is for the let I think it misleads people. Eg if you do `match foo { .. }`, with no let and no semicolon (which is what that rule would suggest), you're actually returning the value of the match. Same goes for a non-match block, too. Eg, `{ .. }` will return the contents. So if you want to prevent that, you use a semicolon. `{ .. };`.
Am I missing something? Why are so many people describing it with the block vs semicolon thing?
Re: Rust Language Cheat Sheet
#30Earlier quoted context omitted.
Technically here the `;` is for the `let`, which is a statement, not for the match expression within it. The braces are part of the match, not of the let. So the let doesn't end with a block, and thus requires a semicolon.
Weird, I thought it was just something like "If it's an expression, use a semicolon otherwise you're returning it". Yet everyone here is making a distinction between blocks and not-blocks. Is "my rule" better? Furthermore, if you say the semicolon is for the let I think it misleads people. Eg if you do `match foo { .. }`, with no let and no semicolon (which is what that rule would suggest) , you're actually returning…