this post was submitted on 16 Oct 2024
65 points (97.1% liked)

Rust

5999 readers
25 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
 

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 11 points 1 month ago (3 children)

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

[–] Ephera@lemmy.ml 7 points 1 month ago (1 children)

This works with anything that one might call "named tuples".

So, you can also define a struct like so and it'll work:

struct Baz(i32);

On the other hand, if you define an enum variant with the normal struct syntax, it does not work:

enum Foo {
    ...
    Qux { something: i32 } //cannot omit braces
}
[–] barsoap@lemm.ee 2 points 1 month ago* (last edited 1 month ago) (1 children)

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)).

[–] Ephera@lemmy.ml 2 points 1 month ago

Yeah, I do miss those, too, although I've noticed that I'm becoming ever more consistent with just naming my variables like the type is called and that works out nicely in Rust, because then you can also leave out the field name when filling in a struct with named fields. I'll often have named my function parameters the same name that I ultimately need to pass into structs fields.

At this point, I'm secretly wondering, if a programming language could be designed where you don't normally fill in variable names, but rather just use the type name to reference each value.
For the few cases where you actually do have multiple variables of the same type, then you could introduce a local (type) alias, much like it's currently optional to add type annotations.
Someone should build this, so I don't have to take on another side project. 🙃

[–] tatterdemalion@programming.dev 3 points 1 month ago

Clippy will warn you if you don't use this feature.

[–] little_ferris@programming.dev 2 points 1 month ago (1 children)

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 (2 children)

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.

[–] anton@lemmy.blahaj.zone 1 points 1 month ago

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.

The fn(T)->R syntax works for functions without associated data, it discards details of the implementation and works like function pointers in C. This allows them to be copy and 'static.

The other function types can have data with them and have more type information at compile time which allows them to be inlined.
These functions each have their own unwritable type that implements the function traits (Fn(T)->R, FnMut(T)->R and FnOnce(T)->R) depending on their enclosed data.

I hope I remembered everything right from this video by Jon Gjengset.