basic/base-type/statement-expression #707
Replies: 20 comments 40 replies
-
main比较特别?在上一节讲他能返回值,也就是表达式,但我let x= main(){}会报错 |
Beta Was this translation helpful? Give feedback.
-
语句返回(),为什么说没有返回值呀? |
Beta Was this translation helpful? Give feedback.
-
fn main() { fn ret_unit_type() {
} ret_unit_type 函数中的 if条件语句,条件不应该加括号🐶 |
Beta Was this translation helpful? Give feedback.
-
parse:: |
Beta Was this translation helpful? Give feedback.
-
return x + 1; 这种形式就可以, 何解呢? |
Beta Was this translation helpful? Give feedback.
-
有个return 就比较傻了,所以不太理解发自我的手机-------- 原始邮件 --------发件人: fchengjin ***@***.***>日期: 2022年5月15日周日 11:19收件人: sunface/rust-course ***@***.***>抄送: _不忘初心 ***@***.***>, Comment ***@***.***>主 题: Re: [sunface/rust-course] basic/base-type/statement-expression (Discussion #707)
上面有写呀,你单独写个9,它是一个表达式,会作为函数的返回值,加上分号就是语句了,没有返回返回值,则函数就默认返回()
—Reply to this email directly, view it on GitHub, or unsubscribe.You are receiving this because you commented.Message ID: ***@***.***>
|
Beta Was this translation helpful? Give feedback.
-
不加分号,a是返回值
那为啥加了等号但是没有分号,却不会反回a, 而是返回单元类型()。是不是=的优先级会高一些
|
Beta Was this translation helpful? Give feedback.
-
{ 这里真的有点反人类,其实大部分语言都是可以这样 |
Beta Was this translation helpful? Give feedback.
-
这个块返回值的特性吹爆,Rust有很多很走心的小特性 |
Beta Was this translation helpful? Give feedback.
-
为什么 |
Beta Was this translation helpful? Give feedback.
-
js之前(2019年左右)有一个 do { 表达式 } ,貌似也是这个路子,叫什么块级作用域返回值的提案,但是这都 2022了,还没通过。。。 |
Beta Was this translation helpful? Give feedback.
-
个人理解,将表达式与语句区分开,主要是避免赋值(=)和逻辑判断(==)混淆的情况。 |
Beta Was this translation helpful? Give feedback.
-
表达式有值,而语句不常有。 |
Beta Was this translation helpful? Give feedback.
-
1 这些表述没有在逻辑上准确地区分概念,按照以上内容的表述,难免造成理解上的含混: 综合以上理由,这一部分的表达比较混乱。造成这个问题可能是因为写的时间比较早? |
Beta Was this translation helpful? Give feedback.
-
个人认为用表达式作为返回值对于第一次见到的人比较震撼,也容易犯错,看代码的时候要主要最后一行有没有分号。但是似乎函数签名可以指定返回值类型以及是否无返回值,可能二者合用是比较好的实践 |
Beta Was this translation helpful? Give feedback.
-
为什么这里 x += 2 也一直都是表达式(statement)? 没有分号不就应该会返回值吗?有没有什么比较好的办法来断定哪些操作是语句哪些操作是返回值? // 使用两种方法让代码工作起来
fn main() {
let v = {
let mut x = 1;
x += 2
};
assert_eq!(v, 3);
} |
Beta Was this translation helpful? Give feedback.
-
可以这样理解么:使用 花括号包裹的语句块就一定是一个表达式? |
Beta Was this translation helpful? Give feedback.
-
总感觉语句块跟表达式(expression)不太一样,虽然语句块也会返回值,但是实际上返回的是语句块中expression的值,比如 fn main() {
{}
{}// 正常
} fn main() {
()
()//报错
} |
Beta Was this translation helpful? Give feedback.
-
报错信息都有注释解释,太太太贴心了。 |
Beta Was this translation helpful? Give feedback.
-
basic/base-type/statement-expression
https://course.rs/basic/base-type/statement-expression.html
Beta Was this translation helpful? Give feedback.
All reactions