Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the correct syntax to return a function in Rust?

What is the correct syntax to return a function in Rust?

The following code does not compile.

  fn identity<T>(a: T) -> T {
    return a;
  };

  fn right<T>(a: T) -> Fn {
    return identity;
  };
like image 967
SmoothTraderKen Avatar asked Jan 26 '26 16:01

SmoothTraderKen


2 Answers

Here (playground) is a minimal example:

fn identity<T>(a: T) -> T {
    return a;
}

fn right<T>(_a: T) -> impl Fn(T) -> T {
    return identity;
}

fn main() {
    println!("{}", right(0)(42))
}

You need to:

  • Specify the input argument and output types within the signature, i.e. Fn(T) -> T.
  • Specify that right's return type implements the trait Fn(T) -> T.

Alternatively, you could also have written the function pointer fn(T) -> T as the return type. Since this is not a trait, you would not need the impl keyword:

fn right<T>(_a: T) -> fn(T) -> T {
    return identity;
}

Only fn items and non-capturing closures can be coerced to function pointers, so, while simpler, this will not work in all cases.

like image 74
Mateen Ulhaq Avatar answered Jan 29 '26 11:01

Mateen Ulhaq


This is the best I came up with:

fn identity<T>(a: T) -> T {
    return a;
}

fn right<T>() -> &'static dyn Fn(T)->T {
    return &identity::<T>;
}

fn main() {
    println!("{}", right()(3.1415));
}

playground <-- test it by yourself.

Explanation

&'static dyn Fn(T)->T means that it returns a reference to some object that satisfy the Fn(T)->T trait (because yes Fn(T)->T is indeed a trait, not a type).

like image 28
uben Avatar answered Jan 29 '26 11:01

uben