Trivia About Rust Types
thecodedmessage.com
thecodedmessage.com
Aside from its actual purpose (as part of making ? less magic, described in this trivia list) it's good to agree how to talk about control flow in the type system. If two unrelated pieces of software both care about control flow, in a lot of languages they'd both make up their own ways to signal "keep going" versus "halt". But since core::ops::ControlFlow exists in Rust it makes sense to use that, and so you spend less time writing adaptors. This is one of the main purposes of a standard library (notice this is in core not just std and so it's available even in a tiny embedded device with no allocator if you want it)
I thought this was so cool, being able to treat Option like a 0/1 collection.
let collection = get_stuff();
for item in collection {
}
This can unexpectedly give you one item of iterable `Result` instead of items of the collection if `get_stuff()` returns a `Result<Vec>` or `Option<Vec>`.Such mistake won't make it to production thanks to strict type checking, but it manifests in surprising and misleading error messages that the `item` doesn't have methods you expect it to have, rather than pointing to the true cause of `get_stuff()` not having error handled.
for x in y.into_iter()
and presumably curse 3836293648 who made them do this extra typing for no purpose.
It doesn't, as you seem to imply fix the parent's problem, because both the container they thought they were getting, and the Option or Result they were actually getting, implement IntoIterator so they'll call into_iter() on it.
Seems like, we do for x in Result-or-Option<T> and T is IntoIterator, and then we treat x as if it is the thing T was an iterator for, rather than T. That's detectable.
So, worth taking some examples of code where you did this, and writing up a report that the compilers team can look at. If you've got some idea what you're doing you might well even find Rust's error reporting code readable enough to dive in and try some fixes yourself.
https://www.arnavion.dev/blog/2020-04-07-rust-the-unfortunat...
I agree that it would be nice if they'd flesh out some of the items with example code though.
I think Jon just intended it as a bit of fun, and so Twitter felt appropriate. After all if there's important stuff to be communicated about a type it should be in the actual documentation. If you want to learn about types entirely by reading Tweets that seems like a weird and probably bad learning style. See also: learning ballet entirely by reading translated poems about ballet.
Although I expect it is overridable in some way but I don't know enough Rust to know how.
There are also alternative vector like types for different use cases, and you can roll your own.
Note: gameswithgo you seem to be shadow banned. Your comment history looks alright tho. You may want to email hn@ycombinator.com asking to have that fixed (edit: found the culprit https://news.ycombinator.com/item?id=30732886)
AIUI that is why NonNull only has one variant- it's just easier to have one kind of pointer. But interestingly, even though `NonNull`'s API is written in terms of `*mut T`, it is implemented in terms of `*const T`: https://github.com/rust-lang/rust/blob/4ca19e09d302a4cbde14f.... This makes it covariant (`NonNull<T>` for `T: 'long` is a subtype of `NonNull<T>` for `T: 'short`), while `*mut T` is invariant, since this is the more common mode, and also the more flexible option that can be restricted back down to invariance when necessary.
So NonNull is sort of a re-setting of the defaults: the unsafe mutability of `*mut T` (since people wind up needing to cast covariant `*const T`s to `*mut T` to write through them a lot anyway), with the covariance of `*const T`.
Details like this let you use the safer and more ergonomic solutions by default rather than having to worry if they are worth the overhead.
You can do a lot of this in languages like C++ but there are some pernicious limits that Rust didn't have e.g. C++ can't conceive of Zero Size Types.
> See the debug_* methods on it.
Sure... please point me to them
Those links would be a great addition to the article, though.
https://doc.rust-lang.org/std/fmt/struct.Formatter.html#meth...
The entire Rust standard library documentation (like most documentation for Rust software, it's built with the same tools) has a Search UI at the top.