Function pointer
C++ :
#include <iostream>
using namespace std;
int eval(int (*f)(int, int), int x, int y)
{
return f(x,y);
}
int main()
{
int a = 1;
int b = 2;
int c = eval([](int x, int y) { return x + y; }, a, b);
cout << a << "+" << b << "=" << c << endl;
return 0;
}
Rust :
fn eval(f: fn(i32,i32)->i32, x : i32, y : i32) -> i32
{
f(x, y)
}
fn main()
{
let a = 1;
let b = 2;
let c = eval(|x,y| x+y, a, b);
println!("{}+{}={}", a, b, c);
}