If we were to create a Rust version of this page for Haskell, what cool programming techniques would you add to it?
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.
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.
Something i didnt know for a long time (even though its mentioned in the book pretty sure) is that enum discriminants work like functions
#[derive(Debug, PartialEq, Eq)] enum Foo { Bar(i32), } let x: Vec<_> = [1, 2, 3] .into_iter() .map(Foo::Bar) .collect(); assert_eq!( x, vec![Foo::Bar(1), Foo::Bar(2), Foo::Bar(3)] );
Not too crazy but its something that blew my mind when i first saw it
Clippy will warn you if you don’t use this feature.
Yea it’s like when we write
Some(2)
. It’s not a function call but a variant of theOption
enum.Enum constructors are functions, this typechecks:
fn foo<T>() { let f: fn(T) -> Option<T> = Some; }
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.
Woah. That’s quite interesting. I didn’t know that.
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 😅.
This is excellent content
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.
What madness caused this
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.
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
Yeah I don’t mean just terse functional idioms. Any programming technique that blew your mind the first time you came across it would qualify.
Type guards, then :) very cool, much compiler power, love it
Do you mean RAII guards
https://rust-unofficial.github.io/patterns/patterns/behavioural/RAII.html
Or match guards?
https://doc.rust-lang.org/rust-by-example/flow_control/match/guard.html
Maybe they’re referring to “where clauses”?
Indeed I am. Forgot the name, lol, not worked with rust for a few months 😅
I’d like to see a Rust solution to Tony Morris’s tic tac toe challenge:
https://blog.tmorris.net/posts/scala-exercise-with-types-and-abstraction/index.html
His rant about it is here:
I did a Haskell GADT solution some time back and it’s supposed to be doable in Java and in C++. Probably Rust too. I wonder about Ada.
That doesn’t look like a particularly difficult challenge? Like, it’s just an implementation game, move returns a data type that you write yourself
Edit: I suppose there’s life in that kind of ambiguous variation though
This could be done almost trivially using the typestate pattern: https://zerotomastery.io/blog/rust-typestate-patterns/.
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.
The haskell examples look more like an arcane wizardry.
My favorite example of haskell arcane wizardry is löb. It’s mentioned in this list but not really done justice imo.
😂 Ikr!
Can’t think of anything.
The novelty must have worn off over time.
Or maybe my mind doesn’t get blown easily.