66
submitted 1 month ago* (last edited 1 month ago) by little_ferris@programming.dev to c/rust@programming.dev

If we were to create a Rust version of this page for Haskell, what cool programming techniques would you add to it?

you are viewing a single comment's thread
view the rest of the comments
[-] tuna@discuss.tchncs.de 12 points 1 month ago

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

[-] little_ferris@programming.dev 2 points 1 month ago

Yea it's like when we writeSome(2). It's not a function call but a variant of the Option enum.

[-] barsoap@lemm.ee 3 points 1 month ago

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.

[-] little_ferris@programming.dev 2 points 1 month ago

Woah. That's quite interesting. I didn't know that.

load more comments (1 replies)
load more comments (1 replies)
load more comments (5 replies)
this post was submitted on 16 Oct 2024
66 points (97.1% liked)

Rust

5999 readers
17 users here now

Welcome to the Rust community! This is a place to discuss about the Rust programming language.

Wormhole

!performance@programming.dev

Credits

  • The icon is a modified version of the official rust logo (changing the colors to a gradient and black background)

founded 1 year ago
MODERATORS