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.
Rust Language Cheat Sheet
41–50 of 65 posts
Re: Rust Language Cheat Sheet
#42Earlier 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…
Re: Rust Language Cheat Sheet
#43Earlier 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 :)
Re: Rust Language Cheat Sheet
#44The 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?
Re: Rust Language Cheat Sheet
#45As 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
#46Re: Rust Language Cheat Sheet
#47Re: Rust Language Cheat Sheet
#48Any plans on adding Box, Rc, RefCell, and the differences thereof? That's the thing that trips me up the most right now.
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
#49Earlier 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 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
#50Earlier 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.