返回闭包

返回闭包
错误例子:

fn returns_closure() -> Fn(i32) -> i32 { |x| x + 1 }
正确例子:

fn returns_closure() -> Box<dyn Fn(i32) -> i32> {
Box::new(|x| x + 1)
}

fn main() {
let c = returns_closure();
println!("r = {}", c(1));
//等价于println!("r = {}", (*c)(1)); //解引用多态
println!("Hello, world!");
}

 

posted on 2020-12-25 20:23  tycoon3  阅读(100)  评论(0编辑  收藏  举报

导航