1

Is it possible to make a type definition for a function not inline, instead standing on its own line?

// a standard fn with it's type inline
pub fn kk<T>(a: &str, b: u32) -> Result<T, &'static str> {
    unimplemented!();
}

// a type alias expressing the same thing as above
type KK<T> = Fn(&str, u32) -> Result<T, &'static str>;

// Question: could I use the type alias above as the type for a function, something like this ↓
fn BB: KK<T> { /* ... */ }
Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
Don Klein
  • 215
  • 2
  • 8
  • 1
    See also [“Expected fn item, found a different fn item” when working with function pointers](https://stackoverflow.com/questions/27895946/expected-fn-item-found-a-different-fn-item-when-working-with-function-pointer), which elaborates a little on the nature of function types – trent Nov 07 '19 at 19:21
  • Thanks for pointing out the answer, so seemed for now it's not possible to do what I expected, I asked because coming from a Haskell background and being spoiled by Hindley–Milner type system for so long that's like the 2nd nature to write out a function type :) – Don Klein Nov 07 '19 at 20:04
  • 1
    You could perhaps express the difference between Haskell and Rust in these terms: In Haskell, a function *has* a type whereas in Rust a function *is* a type (and the only inhabitant of that type, much like `()`). – trent Nov 07 '19 at 20:50

0 Answers0