I found the type alias below in an Scheme interpreter I\'m studying. While evaluating the AST, it recognizes a function either as a natively supported function, or as a user def
The signature of a function describes:
For example, if you define:
fn hello(s: &str) {
println!("Hello {}", s);
}
The function signature is fn hello(&str)
.
In Rust, each function has a unique type, which cannot be named.
However, if you have a function, you can also coerce it into a generic fn
type which does not care about the identity of the function, but only about how it can be used.
For the above function, this generic type is: fn(&str)
(or fn(&str) -> ()
if we wish to be explicit).
This generic type is useful to abstract over multiple functions with a similar signature. For example:
fn add(left: i32, right: i32) -> i32 { left + right }
fn sub(left: i32, right: i32) -> i32 { left - right }
fn select(name: &str) -> fn(i32, i32) -> i32 {
match name {
"add" => add,
"sub" => sub,
_ => unimplemented!(),
}
}
fn main() {
let fun = select("add");
println!("{} + {} = {}", 1, 2, fun(1, 2));
}
It is close to function pointers in C or C++, however unlike function pointers it cannot be null.
If you need a nullable function, you can use Option<fn(i32, i32) -> i32>
instead.
And thus finally we come to this type alias: it's simply a shortcut because the generic fn
type is long. Like any other type alias.