Rust 编程视频教程(进阶)——028_2 返回闭包
2020-03-15 本文已影响0人
令狐壹冲
视频地址
头条地址:https://www.ixigua.com/i6775861706447913485
B站地址:https://www.bilibili.com/video/av81202308/
源码地址
github地址:https://github.com/anonymousGiga/learn_rust
讲解内容
返回闭包
错误例子:
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!");
}