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

  • tuna@discuss.tchncs.de
    link
    fedilink
    arrow-up
    8
    ·
    10 hours 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

      • barsoap@lemm.ee
        link
        fedilink
        arrow-up
        2
        ·
        8 hours 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.