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

And getting into idiomatic Rust + a bit of code golf + a bit of showing-off:

    fn map_on_vec(vec: &[i32], func: F)
    where
        F: Fn(i32) -> i32,
    {
        vec.iter().map(|x| func(*x)).collect()
    }
- There's no need for the temporary variable, can just return directly

- The `vec` argument should be a `&[T]` not a `&Vec<T>` [discouraged]

- The `func` argument shouldn't be a function pointer but instead take an unboxed, generic closure (no forced pointer indirection, can take a closure). You may even want to accept a `FnMut` to allow the closure to mutate itself.

[discouraged]: https://stackoverflow.com/q/40006219/155423



shep! you forgot map_on_vec's return type. Thanks for writing the comment though, I was just about to point out Fn




Consider applying for YC's Fall 2026 batch! Applications are open till July 27.

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

Search: