[][src]Function tao_of_rust::ch02::function::math

pub fn math(op: fn(_: i32, _: i32) -> i32, a: i32, b: i32) -> i32

函数指针: 函数作为参数

Basic usage:

pub fn math(op: fn(i32, i32) -> i32, a: i32, b: i32) -> i32{
    op(a, b)
}
fn sum(a: i32, b: i32) -> i32 {
    a + b
}
fn product(a: i32, b: i32) -> i32 {
    a * b
}

let a = 2;
let b = 3;
assert_eq!(math(sum, a, b), 5);
assert_eq!(math(product, a, b), 6);Run