知识卡片
末尾表达式决定返回值
Rust函数的末尾表达式可以直接作为返回值;加分号把它变成语句后,不能仍假定它返回原表达式的值。未写返回类型的函数返回单元类型(),适合主要产生作用而无有用结果的操作。提前离开函数则可使用return。
这张卡片看不懂?
这张卡片已通过人工审查,不再接受失败反馈。
已有反馈 · 0 条
还没有人提交反馈。
在整书笔记中的位置
原书出处 · 1 处
- Chapter 1. Types Functions and Methods · 查看原书依据
Functions and Methods As with every other programming language, Rust uses functions to organize code into named chunks for reuse, with inputs to the code expressed as parameters. As with every other statically typed language, the types of the parameters and the return value are explicitly specified: /// Return `x` divided by `y`. fn div(x: f64, y: f64) -> f6
参考来源
- 位置:《Effective Rust 35 Specific Ways to Improve Your Rust Code (David Drysdale)》第1章《Functions and Methods》(spine5;extracted/Effective Rust 35 Specific Ways to Improve Your Rust Code (David Drysdale)/_epub-src/OEBPS/ch01.html;u016-ch01-012)。
- 结论依据:原文在“Functions and Methods”附近的定义、例子或代码支撑本卡核心说明:Rust函数的末尾表达式可以直接作为返回值;加分号把它变成语句后,不能仍假定它返回原表达式的值。边界按已读前提解释;示例未执行。
- 原始内容:Functions and Methods As with every other programming language, Rust uses functions to organize code into named chunks for reuse, with inputs to the code expressed as parameters. As with every other statically typed language, the types of the parameters and the return value are explicitly specified: /// Return `x` divided by `y`. fn div(x: f64, y: f64) -> f6