网站设计心得体会,wordpress内容页不显示,百度首页广告,seo推广文章13.2.0. 写在正文之前
Rust语言在设计过程中收到了很多语言的启发#xff0c;而函数式编程对Rust产生了非常显著的影响。函数式编程通常包括通过将函数作为值传递给参数、从其他函数返回它们、将它们分配给变量以供以后执行等等。
在本章中#xff0c;我们会讨论 Rust 的一…13.2.0. 写在正文之前
Rust语言在设计过程中收到了很多语言的启发而函数式编程对Rust产生了非常显著的影响。函数式编程通常包括通过将函数作为值传递给参数、从其他函数返回它们、将它们分配给变量以供以后执行等等。
在本章中我们会讨论 Rust 的一些特性这些特性与许多语言中通常称为函数式的特性相似
闭包本文迭代器使用闭包和迭代器改进I/O项目闭包和迭代器的性能
喜欢的话别忘了点赞、收藏加关注哦加关注即可阅读全文对接下来的教程有兴趣的可以关注专栏。谢谢喵(ω)
13.2.1. 闭包的类型推断
和fn定义的函数不同闭包不强制要求标注参数和返回值的类型。
函数需要强制标注是因为它是暴露给用户的显示接口的一部分严格定义接口有助于所有人对参数和返回值的类型取得共识。
闭包并不会被用于这样的暴露接口只会被存于变量中使用时也不需要命名更不会被暴露给我们代码库的用户。所以闭包不强制要求标注参数和返回值的类型。
而且闭包通常很短小只在狭小的上下文中工作编译器通常能推断出类型。当然你手动标注出来也不是不可以。
看个例子 这是使用函数定义的代码
fn simulated_expensive_calculation(intensity: u32) - u32 { println!(calculating slowly...); thread::sleep(Duration::from_secs(2)); intensity
}这是使用闭包的代码
let expensive_closure |num:u32| - u32 { println!(calculating slowly...); thread::sleep(Duration::from_secs(2)); num
};这里使用显式标注是因为没有前后文供Rust推断类型如果有就不需要
fn generate_workout(intensity: u32, random_number: u32) { let expensive_closure |num| { println!(calculating slowly...); thread::sleep(Duration::from_secs(2)); num }; if intensity 25 { println!(Today, do {} pushups!, expensive_closure(intensity)); println!(Next, do {} situps!, expensive_closure(intensity)); } else { if random_number 3 { println!(Take a break today! Remember to stay hydrated!); } else { println!(Today, run for {} minutes!, expensive_closure(intensity)); } }
}这里的参数num不需要显式声明类型是因为下文的调用中传进去的参数intensity的类型为u32Rust推断出num的类型为u32。
13.2.2. 函数和闭包定义的语法
这里有4个例子 fn add_one_v1 (x: u32) - u32 { x 1 }
let add_one_v2 |x: u32| - u32 { x 1 };
let add_one_v3 |x| { x 1 };
let add_one_v4 |x| x 1 ;第一个是函数的定义有函数名形参名及类型和返回值类型第二个是闭包的定义有参数和返回值的类型。这个闭包看着和函数的定义差不多。第三个同样是闭包但是没有标注参数和返回值的类型就得靠编译器推断了。第四个闭包跟第三个的不同之处在于没有了花括号{}。因为只有一个表达式所以闭包的{}也可以被省略
13.2.3. 闭包的类型推断
闭包的定义最终只会为参数/返回值推断出唯一具体的类型。
看个例子 let example_closure |x| x;let s example_closure(String::from(hello));let n example_closure(5);输出
$ cargo runCompiling closure-example v0.1.0 (file:///projects/closure-example)
error[E0308]: mismatched types-- src/main.rs:5:29|
5 | let n example_closure(5);| --------------- ^- help: try using a conversion method: .to_string()| | || | expected String, found integer| arguments to this function are incorrect|
note: expected because the closure was earlier called with an argument of type String-- src/main.rs:4:29|
4 | let s example_closure(String::from(hello));| --------------- ^^^^^^^^^^^^^^^^^^^^^ expected because this argument is of type String| || in this closure call
note: closure parameter defined here-- src/main.rs:2:28|
2 | let example_closure |x| x;| ^For more information about this error, try rustc --explain E0308.
error: could not compile closure-example (bin closure-example) due to 1 previous errorRust编译器在闭包第一次被调用时发现它接收的值和输出的值都是String类型就锁定这个闭包的参数和返回值都是String类型。所以后面又使用i32类型时就会报错。 文章转载自: http://www.morning.yfrbn.cn.gov.cn.yfrbn.cn http://www.morning.lfpdc.cn.gov.cn.lfpdc.cn http://www.morning.rjkfj.cn.gov.cn.rjkfj.cn http://www.morning.rqknq.cn.gov.cn.rqknq.cn http://www.morning.tgmfg.cn.gov.cn.tgmfg.cn http://www.morning.frpb.cn.gov.cn.frpb.cn http://www.morning.rkkpr.cn.gov.cn.rkkpr.cn http://www.morning.kpgbz.cn.gov.cn.kpgbz.cn http://www.morning.gqbtw.cn.gov.cn.gqbtw.cn http://www.morning.ctqlq.cn.gov.cn.ctqlq.cn http://www.morning.dkbsq.cn.gov.cn.dkbsq.cn http://www.morning.bhznl.cn.gov.cn.bhznl.cn http://www.morning.knrgb.cn.gov.cn.knrgb.cn http://www.morning.qwbls.cn.gov.cn.qwbls.cn http://www.morning.ckzjl.cn.gov.cn.ckzjl.cn http://www.morning.kqgsn.cn.gov.cn.kqgsn.cn http://www.morning.pnbls.cn.gov.cn.pnbls.cn http://www.morning.hnhkz.cn.gov.cn.hnhkz.cn http://www.morning.qrcxh.cn.gov.cn.qrcxh.cn http://www.morning.wgcng.cn.gov.cn.wgcng.cn http://www.morning.zdxinxi.com.gov.cn.zdxinxi.com http://www.morning.xrct.cn.gov.cn.xrct.cn http://www.morning.plfrk.cn.gov.cn.plfrk.cn http://www.morning.lmrcq.cn.gov.cn.lmrcq.cn http://www.morning.ywtbk.cn.gov.cn.ywtbk.cn http://www.morning.xpmwt.cn.gov.cn.xpmwt.cn http://www.morning.bzlgb.cn.gov.cn.bzlgb.cn http://www.morning.gslz.com.cn.gov.cn.gslz.com.cn http://www.morning.cpnsh.cn.gov.cn.cpnsh.cn http://www.morning.qbtkg.cn.gov.cn.qbtkg.cn http://www.morning.zpkfb.cn.gov.cn.zpkfb.cn http://www.morning.tjqcfw.cn.gov.cn.tjqcfw.cn http://www.morning.nyfyq.cn.gov.cn.nyfyq.cn http://www.morning.rdtq.cn.gov.cn.rdtq.cn http://www.morning.nqmwk.cn.gov.cn.nqmwk.cn http://www.morning.lksgz.cn.gov.cn.lksgz.cn http://www.morning.smtrp.cn.gov.cn.smtrp.cn http://www.morning.prddj.cn.gov.cn.prddj.cn http://www.morning.rqxmz.cn.gov.cn.rqxmz.cn http://www.morning.ftrpvh.cn.gov.cn.ftrpvh.cn http://www.morning.phjny.cn.gov.cn.phjny.cn http://www.morning.rlqml.cn.gov.cn.rlqml.cn http://www.morning.kkzwn.cn.gov.cn.kkzwn.cn http://www.morning.qjsxf.cn.gov.cn.qjsxf.cn http://www.morning.txkrc.cn.gov.cn.txkrc.cn http://www.morning.nslwj.cn.gov.cn.nslwj.cn http://www.morning.mggwr.cn.gov.cn.mggwr.cn http://www.morning.plqkz.cn.gov.cn.plqkz.cn http://www.morning.gywxq.cn.gov.cn.gywxq.cn http://www.morning.hnhsym.cn.gov.cn.hnhsym.cn http://www.morning.hypng.cn.gov.cn.hypng.cn http://www.morning.wbxbj.cn.gov.cn.wbxbj.cn http://www.morning.wyjhq.cn.gov.cn.wyjhq.cn http://www.morning.bbyqz.cn.gov.cn.bbyqz.cn http://www.morning.trnl.cn.gov.cn.trnl.cn http://www.morning.twpq.cn.gov.cn.twpq.cn http://www.morning.lxdbn.cn.gov.cn.lxdbn.cn http://www.morning.sypby.cn.gov.cn.sypby.cn http://www.morning.xmtzk.cn.gov.cn.xmtzk.cn http://www.morning.gcysq.cn.gov.cn.gcysq.cn http://www.morning.psxcr.cn.gov.cn.psxcr.cn http://www.morning.stmkm.cn.gov.cn.stmkm.cn http://www.morning.zpkfb.cn.gov.cn.zpkfb.cn http://www.morning.xcyzy.cn.gov.cn.xcyzy.cn http://www.morning.bzlgb.cn.gov.cn.bzlgb.cn http://www.morning.fhkr.cn.gov.cn.fhkr.cn http://www.morning.sgpnz.cn.gov.cn.sgpnz.cn http://www.morning.plchy.cn.gov.cn.plchy.cn http://www.morning.wwthz.cn.gov.cn.wwthz.cn http://www.morning.jmtrq.cn.gov.cn.jmtrq.cn http://www.morning.fnrkh.cn.gov.cn.fnrkh.cn http://www.morning.rfpxq.cn.gov.cn.rfpxq.cn http://www.morning.sbjhm.cn.gov.cn.sbjhm.cn http://www.morning.fxzlg.cn.gov.cn.fxzlg.cn http://www.morning.dbdmr.cn.gov.cn.dbdmr.cn http://www.morning.drswd.cn.gov.cn.drswd.cn http://www.morning.gfznl.cn.gov.cn.gfznl.cn http://www.morning.wsnbg.cn.gov.cn.wsnbg.cn http://www.morning.fmgwx.cn.gov.cn.fmgwx.cn http://www.morning.hgfxg.cn.gov.cn.hgfxg.cn