Live data from Hacker News

Rust Language Cheat Sheet

cheats.rs

41–50 of 65 posts

Re: Rust Language Cheat Sheet

#41

As 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?

They appear to be similar to the way most other languages like C and Java use semicolons, where a semicolon isn't required after a code block (generally denoted by squiggly brackets), but is required at the end of most (all?) other statements.

The most confusing thing ever is, of course, the fact that C struct/union and C++ class declarations do require a semicolon after the closing curly bracket. This is because type declarations are permitted to occur inline in certain contexts, notably as the type specifier in a variable declaration.

Re: Rust Language Cheat Sheet

#42

Earlier quoted context omitted.

TBH I thought it made sense, but I'm still confused on everyone elses description lol. I feel like what you said is what I said, but with more accuracy and depth. Eg, if my block was: { foo(); bar() baz(); } My statement holds true, no? It's obviously a syntax problem, since I'm trying to define a statement after I define an expression (which would be trying to execute code after a return, effectively) . So.. I'm not…

It is not 100% clear what specifically you're saying to me, to be honest. All I know is, that in the code above, if you expect bar() to return its value from the block, your mental model of how this stuff works is incorrect. To be honest, my mental model of all of this is "write code, fix it when the compiler complains"; I rarely think about semicolons, and cargo fmt will remove extraneous ones, so it's sometimes har…

Apologies for the poorly worded question. Regardless, thanks much as always :)

Re: Rust Language Cheat Sheet

#43

Earlier quoted context omitted.

It is not 100% clear what specifically you're saying to me, to be honest. All I know is, that in the code above, if you expect bar() to return its value from the block, your mental model of how this stuff works is incorrect. To be honest, my mental model of all of this is "write code, fix it when the compiler complains"; I rarely think about semicolons, and cargo fmt will remove extraneous ones, so it's sometimes har…

Apologies for the poorly worded question. Regardless, thanks much as always :)

I don't think it was poorly worded; I'm just chugging some coffee and find some discussions easier than others :)

Re: Rust Language Cheat Sheet

#44
post #6

The explanation of lifetimes linked here is really great - I think this goes a little further than just a cheat sheet. Seems to be more like excellent Clif notes.

Lifetimes are about where I bowed out of Rust. I haven't worked with C++ before, or really ever thought about borrowing or lifetimes, so it never really clicked in my head. What's worse is the Rust community's desire to remind you to "Just read the book". Maybe this would be a worthwhile read to get back into it?

The Book by Steve Klabnik looks fantastic for newer programmers, but as an experienced programmer I did find it difficult to get into. On the other hand, the O’Reilly book by Jim Blandy and Jason Orendorff is just amazing for experienced programmers (and would be unsuitable for new programmers). It’s the best programming language book I’ve ever read. It’s great that both books exist, filling those two niches.

Re: Rust Language Cheat Sheet

#45

As 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?

On the practical side, assuming you set up your editor to run rustfmt on save, then you don't have to think about it because rustfmt will insert delete semicolons if you get it wrong, and then you end up learning anyway.

Re: Rust Language Cheat Sheet

#47
Pretty solid list, only thing I saw missing was binary representation for literals("0b0000_1000") which is really handy when working with micros or other things that use a lot of flags or bits.

Re: Rust Language Cheat Sheet

#48

Any plans on adding Box, Rc, RefCell, and the differences thereof? That's the thing that trips me up the most right now.

To give you a quick summary:

Box is (mostly) used in two specific circumstances: when you want to have ownership of something that lives longer than a single function, and to create heterogeneous lists of things (a "trait object").

Rc is used when you need multiple ownership of something, and you're not going to be sharing those things between threads.

RefCell is a type that moves the borrow checker to runtime; it's useful when you're trying to mutate something, and Rust's static checking can't tell that it's okay. If you mess it up, you'll get a panic. This often happens in combination with Rc, since Rc makes something have multiple owners.

Re: Rust Language Cheat Sheet

#49

Earlier quoted context omitted.

You’re not alone. I found the Rust book very hard going, whereas Programming Rust was just the right amount of rigor and interesting examples for me.

This is why I'm glad we have both; I don't think one resource could ever satisfy everyone. Pumped to see Rust in Action getting closer to publication too!

I just bought Rust in Action a couple weeks ago (after having gone up to chapter 11 in the No Starch book). It's definitely good to have both. I'll probably do 1/2 the new book, finish the 1st one and then finish the new one.

I did pretty much the same thing with Dave Thomas's Programming Elixir book, bailing halfway through, using a 2nd resource to consolidate, and then returning to finish it.

I like learning Rust so far. The main thing I wonder is what kinds of projects are good to do while learning. I went from Node to Rails to Phoenix and normally I just rewrite web apps I've previously created in the language I'm learning. With Rust, it feels like I should build something different, something lower-level like a tool, an emulator or a 2D game.

Re: Rust Language Cheat Sheet

#50

Earlier quoted context omitted.

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…

You’re not alone. I found the Rust book very hard going, whereas Programming Rust was just the right amount of rigor and interesting examples for me.

Seconded. I read Programming Rust and it was excellent. Too bad there is no second part. I didn't get too far in the Rust book though.
Post reply on HN