little_ferris@programming.dev to Rust@programming.devEnglish · edit-215 hours agoWhat are some mind blowing Rust tricks?message-squaremessage-square26fedilinkarrow-up147arrow-down11file-text
arrow-up146arrow-down1message-squareWhat are some mind blowing Rust tricks?little_ferris@programming.dev to Rust@programming.devEnglish · edit-215 hours agomessage-square26fedilinkfile-text
If we were to create a Rust version of this page for Haskell, what cool programming techniques would you add to it?
minus-squaretuna@discuss.tchncs.delinkfedilinkarrow-up8·10 hours agoSomething 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
minus-squaretatterdemalion@programming.devlinkfedilinkarrow-up2·5 hours agoClippy will warn you if you don’t use this feature.
minus-squarelittle_ferris@programming.devOPlinkfedilinkEnglisharrow-up1·8 hours agoYea it’s like when we writeSome(2). It’s not a function call but a variant of the Option enum.
minus-squarebarsoap@lemm.eelinkfedilinkarrow-up2·8 hours agoEnum 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.
minus-squarelittle_ferris@programming.devOPlinkfedilinkEnglisharrow-up1·7 hours agoWoah. That’s quite interesting. I didn’t know that.
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.