知识卡片
闭包的类型可由上下文推断
把 |x| x % 2 == 0 绑定为 is_even,就能用 is_even(14) 调用它。闭包参数写在竖线之间,编译器通常可推断参数和返回类型。显式声明返回类型时,函数体要写成块,如 |x: u64| -> bool { x % 2 == 0 }。
这张卡片看不懂?
这张卡片已通过人工审查,不再接受失败反馈。
已有反馈 · 0 条
还没有人提交反馈。
在整书笔记中的位置
原书出处 · 1 处
- Expressions 用闭包表达可传递的小函数 · 查看原书依据
Rust infers the argument types and return type. You can also write them out explicitly, as you would for a function.
参考来源
- 位置:《Programming Rust Fast, Safe Systems Development, 3rd ed. (First Early Release)》第05章《Expressions》|用闭包表达可传递的小函数,来源项目 `book-source`,`extracted/Programming Rust Fast, Safe Systems Development, 3rd ed. (First Early Release)/_epub-src/OEBPS/ch05.xhtml#expressions-closures`
- 结论依据:原文比较有、无块体的显式返回类型声明,并展示和函数相同的调用写法。
- 原始内容:Rust infers the argument types and return type. You can also write them out explicitly, as you would for a function.