Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

I think you just want Rust.

You can write extremely haskell-like code with Rust.

Here's the first example in rust:

    fn safe_head<T>(list: &[T]) -> Option<&T> {
        match list {
            [first, ..] => Some(first),  
            [] => None,                  
        }
    }

    fn print_the_first_thing(my_list: &[String]) {
        match safe_head(my_list) {
            Some(something) => println!("{}", something),
            None => println!("You don't have any favourite things? How sad."),
        }
    }

    fn main() {
        let my_favourite_things = vec!["raindrops on roses".to_string(), "whiskers on kittens".to_string()];
        let empty_list: Vec<String> = vec![];

        print_the_first_thing(&my_favourite_things);
    
        print_the_first_thing(&empty_list);
    }
(of course Rust has a lot of helper functions that avoid all that verbosity -- you can do the whole thing in one expression, if you want)

    println!(
        "{}",
        my_favourite_things.get(0).map_or(
            "You don't have any favourite things? How sad.".to_string(),
            |something| something.to_string()
        )
    );


Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: