Not exactly the same thing but this is still pretty funny. This is code that is technically 100% legal Rust but you should definitely never write such code đ .
It's a test for the compiler which ensures that these legal yet extremely weird expressions continue to compile as the compiler is updated. So there is a purpose to the madness but it does still look pretty funny.
That makes complete sense. Ranges implement fmt::Debug, .. is a range, in particular the full range (all values) ..= isn't because the upper bound is missing but ..=.. ranges from the beginning to the... full range. Which doesn't make sense semantically but you can debug print it so add a couple more nested calls and you get a punch card.
I totally didn't need the Rust playground to figure that out.
EDIT: Oh, glossed over that: .. is only the full range if standing alone, it's also an infix operator which is why you can add as many as you want (be careful with whitespace, though). .. .. .. .. .. .. .. .. .. .. is a valid Rust expression.
A cool thing you can do, is to store values of all kinds of types in a big HashMap, basically by storing their TypeId and casting them to Box<dyn Any> (see std::any).
Then you can later retrieve those values by specifying the type (and optionally another ID for storing multiple values of the same type).
So, you can create an API which is used like this:
let value = MyType::new();
storage.insert(value);
let retrieved = storage.get::<MyType>();
assert_eq!(retrieved, value);
There's various ECS storage libraries which also implement such an API. Depending on what you're doing, you might prefer to use those rather than implementing it yourself, but it's not too difficult to implement yourself.
Rust isn't really a language that lends itself to terse point-free functional idioms... The sort of examples I might want to share would probably require a bit more context, certainly more code. Like I think type guards and arena allocation are cool and useful tricks but I don't think I could write a neat little example showing or motivating either
Named function arguments would occasionally be nice to have instead of the single n-tuple they take now. Currently I'm more or less playing a game of "can I name my local variables such that rust-analyzer won't display the argument name when I stick them into functions (because they're called the same)).
I was a bit apprehensive because rust has like a gazillion different function types but here it seems to work like just any other language with a HM type system.
One thing I like a lot about Rust is that it rarely does blow my mind.
But one crate that actually did blow my mind is corosensei. It's not Rust per se that is so crazy here, but the way it's essentially implementing a new language feature with assembly code. This is how you know Rust really is a systems programming language. I invite you to read the source code.
Oh wow I've been looking for something nice like that for ages. Python can do this and it's really great for silicon verification test stimulus. I've also done it in C++ using C++20 coroutines, but they are so complicated and low level I ended up having to use a library to help (libcoro). Felt like a bit of a gap in Rust, but this looks like a great solution!
Mindblowing features are basically, by definition, a result of bad language design. They blow your mind, since they are totally unexpected behaviours. They may still be cool, but they are unexpected and hence unintuitive.
A language that are full of these is Perl. And one simple one is that you can take the string "AAAAA" and use addition on that, like "AAAAA"++ and you will get the result "AAAAB". Cool you may think, but is it really? Addition is normally used to increase the value of a number, that is a completely different operation than modifying a String. The string "AAAAA" cannot be said to be greater or less than "AAAAB", besides the very special case when we order it. But in general the name "John" is not considered to be higher/lower than "Mark", they are just different. So, even if it is cool to manipulate strings by using addition/subtraction, it is still bad language design and very unintuitive. Also, since perl is so loosely typed, it may also cause very unexpected bugs.
strcmp() returns an integer indicating the result of the comparison, as follows:
0, if the s1 and s2 are equal;
a negative value if s1 is less than s2;
a positive value if s1 is greater than s2.
So basically, if C had higher level constructs, it would be identical to Rust here.
So, even if it is cool to manipulate strings by using addition/subtraction, it is still bad language design and very unintuitive.
Rust has impl Add<&str> for String and impl AddAssign<&str> for String. Both append as expected.
But maybe you meant numeric addition specifically.
In that case, yes, Rust doesn't have that, although it's an impl<'a> Step for &'a str away from having something similar (it would be ("AAAAA"..).next()).
impl Step for char already exists of course, but shouldn't if we take your argument to its logical conclusion.
Oh, and C would most definitely have this feature if it could. Numerical manipulation of chars is commonplace there.
Rust has impl Add<&str> for String and impl AddAssign<&str> for String. Both append as expected.
I wouldn't go so far and say "as expected": "Addition" implies that we're dealing with a ring, that there's also multiplication, and that really doesn't make sense for strings (unless you indeed consider them numbers). It's why Haskell's Monoid typeclass comes with the function mappend, not madd.
In Rust's defence though std::ops traits aren't meant to carry any semantics they're about syntax: It's called Add because it's +, not because it means something. I do think it would've been worth it to introduce ++ for general appending (also vectors, sets, maps, etc), though, to keep things clean. Also ++= for the mutating case.
You can manually implement PartialEq and Eq on an Enum that implements Hash to manually determine how the hashmap keys override/collide with one another.
Neat that looks interesting. There's a similar Haskell idiom called session types. I have a bit of reservation about whether one can easily use Rust traits to mark out the permissible state sets that an operation can take, but that's because I don't know Rust at all. I do remember doing a hacky thing with TypeLits in Haskell to handle that. Basically you can have numbers in the type signatures and do some limited arithmetic with them at type level. I'd be interested to know if that is doable in Rust.
Here's another Haskell example where I'd be interested to see a Rust counterpart. It's a red-black tree implementation where the tree invariants are enforced by types. The code would less ugly with more recent GHC features, but same idea.