Jon Gjengset Profile picture
Jun 22 39 tweets 12 min read
Nuggets about each and every (strict) @rustlang keyword — let's go! 1/39
doc.rust-lang.org/stable/referen…
`as`: `x as T` can do lossy conversion, so prefer `T::try_from(x).expect("…")` if you want it to fail loudly (e.g., if a u64 doesn't fit in a u32). 2/39
rust-lang.github.io/rust-clippy/ma…
`async`: in `async` blocks, `return` and `?` only bubble to the result of the future, whereas `break` and `continue` would affect the outer context and so re disallowed. 3/39
doc.rust-lang.org/stable/referen…
`await`: `await` is nothing but syntactic sugar for a `loop` that calls `Future::poll` and calls `yield` on `Poll::Pending`. 4/39
github.com/rust-lang/rust…
`break`: You can pass a value to break in a `loop` to make the `loop` evaluate to that value. 5/39
doc.rust-lang.org/stable/referen…
`const`: There are a _lot_ more constifications planned, including allowing `async {}` in `const fn`! 6/39
github.com/rust-lang/rust…
`continue`: `continue` effectively evaluates to `!`, which allows it to masquerade as any type. 7/39
play.rust-lang.org/?version=stabl…
`crate`: These days, the two primary uses of `crate` as a standalone keyword have mostly gone away: `extern crate` is rarely used, and `crate` as a visibility modifer won't happen (just use `pub(crate)` instead). 8/39
github.com/rust-lang/rust…
`dyn`: `dyn` trait objects come with a default lifetime bound inherited from the containing wide pointer type. 9/39
doc.rust-lang.org/stable/referen…
`else`: `else` is about to get another meaning with let-else expressions, which are like `.unwrap_or` for pattern matches! 10/39
github.com/rust-lang/rust…
`enum`: For data-less enums ("C-style enums"), you can assign a value to only a _subset_ of the variants, and the remaining ones get inferred automatically. 11/39
doc.rust-lang.org/stable/referen…
`extern`: `extern` on its own is an alias for `extern "C"` — the two are interchangeable. 12/39
doc.rust-lang.org/stable/referen…
`false`: `false < true`. 13/39
doc.rust-lang.org/stable/referen…
`fn`: You can write `#![attr]` immediately _inside_ a function to add attributes to the function itself (though it's not very idiomatic). 14/39
doc.rust-lang.org/stable/referen…
`for`: `for` loops are really just syntax sugar for a `loop` that calls `IntoIterator::into_iter` and then matches on `Iterator::next`. 15/39
github.com/rust-lang/rust…
`if`: 🤔🤷 16/39
`impl`: You can put `const`s directly in `impl` blocks to make "associated constants" for a type. 17/39
doc.rust-lang.org/stable/referen…
`in`: Not specifically about `in`, but any `..` expression is actually just a convenient constructor for a `Range` type. 18/39
doc.rust-lang.org/stable/referen…
`let`: The interaction between `let _ ` and dropping are subtle and interesting. 19/39
github.com/rust-lang/rust…
`loop`: There used to be a mode in the Rust compiler that turned every function body into `loop {}`, which was used (among other things) to make `rustdoc` faster. It was called "everybody loops" 😆. 20/39
github.com/rust-lang/rust…
`match`: The expression that a match matches on is called a "scrutinee". 21/39
doc.rust-lang.org/stable/referen…
`mod`: You can make `mod` load files from non-standard locations using the `#[path]` attribute. Though use it sparingly, as it can be very confusing for other developers trying to sort through your code base. 22/39
doc.rust-lang.org/stable/referen…
`move`: The "opposite" capture behavior of `move` (that is, if `move` _isn't_ passed), is far from straightforward. 23/39
doc.rust-lang.org/stable/referen…
`mut`: While `mut` is *techically* short for "mutable", you're generally better off thinking about it as "MUTually exclusive". 24/39
docs.rs/dtolnay/latest…
`pub`: There are a lot of visibility modifiers between non-`pub` and `pub`. `pub(crate)` and `pub(super)` are both useful, but you can even specify `pub(in crate::some_mod)` to get really specific. 25/39
doc.rust-lang.org/stable/referen…
`ref`: You can use `ref` to match something as a reference in a pattern, instead of capturing it as a value. That's not a secret of `ref`, it's just what it means, but it's so commonly confused that it's worth repeating. 26/39
doc.rust-lang.org/stable/referen…
`return`: `return` has a secret, long-unimplemented sibling, `become`, which is intended to one day maybe be used for functions that require guaranteed tail-call optimization. 27/39
rust-lang.github.io/rfcs/0601-repl…
`self`: `self` is special when it comes to automatic inference of the lifetime of return values. Specifically, the lifetime of a `&self` or `&mut self` argument will always be inferred to be the lifetime of the return value (if lifetimes are elided). 28/39
doc.rust-lang.org/stable/referen…
`static`: You can declare `static` items inside function bodies to get a `static` that only that function has access to (unless it gives out references). This can be handy for implementing singleton-like patterns. 30/39
doc.rust-lang.org/stable/referen…
`struct`: A tuple-like struct implicitly defines a free-standing function by the same name that acts as the type's constructor. And a unit-like struct implicitly defines a constant by that name! 31/39
doc.rust-lang.org/stable/referen…
`super`: `super` is only permitted to follow either another `super` or an initial `self` in paths, and if it follows a `self` then the `self` is effectively ignored. 32/39
doc.rust-lang.org/stable/referen…
`trait`: Traits can have associated constants, both with and without default values. 33/39
doc.rust-lang.org/stable/referen…
`true`: Even though bools in Rust are generally `u8`s internally, it is illegal for `true` to be represented by anything but `0x01`. This is to enable optimizations that use other bitpatterns to compress state (like Option<bool>::None == 0x02). 34/39
github.com/rust-lang/rfcs…
`type`: You cannot construct a type through its type alias name. 35/39
doc.rust-lang.org/stable/referen…
`unsafe`: You don't currently have to use `unsafe {}` to perform unsafe operations inside an `unsafe fn`, though that's likely to get a lint in the future to encourage fine-grained safety tracking (and thinking). 36/39
github.com/rust-lang/rust…
`use`: You can use the syntax `use path::to::Trait as _` to bring a trait into scope (so you can call its methods) without also bringing the name into scope. Handy in cases where two traits have the same name for example. 37/39
doc.rust-lang.org/stable/referen…
`where`: `where` clauses do not _need_ to reference the generic type parameters of the thing they're attached to. You can write, for example: `where String: Clone` if you want to. 38/39
doc.rust-lang.org/stable/referen…
`while`: While loops are particularly handy for cases where the iterator type is useful beyond iteration. For example, if you're iterating over a `Path` (with `Path::iter`), you can call `.as_path()` on the iterator to get the _remaining_ `Path`. 39/39
doc.rust-lang.org/std/path/struc…

• • •

Missing some Tweet in this thread? You can try to force a refresh
 

Keep Current with Jon Gjengset

Jon Gjengset Profile picture

Stay in touch and get notified when new unrolls are available from this author!

Read all threads

This Thread may be Removed Anytime!

PDF

Twitter may remove this content at anytime! Save it as PDF for later use!

Try unrolling a thread yourself!

how to unroll video
  1. Follow @ThreadReaderApp to mention us!

  2. From a Twitter thread mention us with a keyword "unroll"
@threadreaderapp unroll

Practice here first or read more on our help page!

More from @jonhoo

Jun 3
Okay, learning time! Name a @rustlang type (can be generic), and I'll (try to) tell you something you didn't know about that type!
I'm guessing this will be a learning experience as much for me too, as I'm sure there are some I'll have to dig a bit to get at something new and interesting for 😅
Also, RIP my mentions
Read 5 tweets
Nov 5, 2021
I had a conversation about how to get signals about the health of and love for open-source projects recently, and had this (potentially erroneous) thought: bug reports are positive signals, while feature requests are negative.
A bug report means that a) someone is using your project, b) they care enough to file an issue, and c) they didn't just immediately switch to another project instead.
A feature request _also_ means that someone is using your project and care enough to file, but is additionally an indicator that their needs aren't currently (completely) served by the project. That's not the be-all and end-all, but it _is_ an indication of pain.
Read 8 tweets
Jun 1, 2021
Big announcement time 📢 For a while now, I've been working on a book that covers the next steps of @rustlang after "the book" — everything from API design to error handling to concurrency to async to FFI. And the early access was just released over at nostarch.com/rust-rustaceans! 🎉
The book is, as its name implies, written for people who are already familiar with Rust. The idea is that you read The Rust Programming Language first, play around with Rust for a bit on your own, maybe start using it "for real", and then pick this up to hone your skills.
It's written so that each chapter is more or less standalone, so you can read up on the subjects you want to know more about, rather than having to read through the whole thing start-to-finish. Though of course you can do that too if you want!
Read 7 tweets

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just two indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3/month or $30/year) and get exclusive features!

Become Premium

Don't want to be a Premium member but still want to support us?

Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal

Or Donate anonymously using crypto!

Ethereum

0xfe58350B80634f60Fa6Dc149a72b4DFbc17D341E copy

Bitcoin

3ATGMxNzCUFzxpMCHL5sWSt4DVtS8UqXpi copy

Thank you for your support!

Follow Us on Twitter!

:(