I have a collection of different sort functions with a common signature so they can be passed interchangeably to other functions. I want to be able to declare inline that each should match the same signature, so I'll be warned contextually if one of them breaks with it.
Rust has a syntax for declaring function types:
type Foo = Fn(i32) -> i32;
fn bar(func: &Foo) {
func(12);
}
What I can't figure out is how to declare a regular (non-closure) function that adheres to a function type:
// this works and can be passed as a Foo, but
// duplicates code and isn't checked against Foo
fn blah(x: i32) -> i32 {
return x * 2;
}
// this isn't valid syntax
fn blah(x): Foo {
return x * 2;
}
This is common practice in some other languages that have function types. Is there a syntax I don't know about, is this an upcoming feature, or is there some technical reason preventing it from being added to Rust?
Note; something like this would also serve my purpose, even though it'd be more clunky:
fn blah(x: i32) -> i32 {
return x * 2;
}: Foo