Hacker News new | past | comments | ask | show | jobs | submit login

For brevity, doesn't type inference work in this case? (I'm not up to speed on the language evolution).

fn heresy() -> { "I am the corruption lurking in your heart" }




Graydon (the Rust BDFL) has always been adamant about being explicit about types in function signatures, so no, you can't infer the return type there. I personally agree with this decision, since such an important part of using any API is knowing exactly what to put in and exactly what you're getting out.

However, closures are allowed to have all of their types inferred. This is because closures aren't allowed to exist at the top level of a file (in what's called "item position"), and so you don't really have to worry about them being used by others. The closure equivalent of the heresy example would be:

  // Note that the braces in this line are optional
  let heresy = || { "I am the corruption lurking in your heart" };
  // Though you're still allowed to be explicit, if you'd like
  let heresy = || -> &str { "I am the corruption lurking in your heart" };




Consider applying for YC's Spring batch! Applications are open till Feb 11.

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

Search: