-
Notifications
You must be signed in to change notification settings - Fork 1.1k
basic-types/statements-expressions #183
-
basic-types/statements-expressions
Learning Rust By Practice, narrowing the gap between beginner and skilled-dev with challenging examples, exercises and projects.
https://zh.practice.rs/basic-types/statements-expressions.html
Beta Was this translation helpful? Give feedback.
All reactions
-
👍 45 -
😄 4 -
🎉 5 -
❤️ 8 -
🚀 13 -
👀 4
Replies: 80 comments 22 replies
-
完成 3/3
Beta Was this translation helpful? Give feedback.
All reactions
-
👍 4 -
❤️ 1
-
nice practice!
Beta Was this translation helpful? Give feedback.
All reactions
-
done
Beta Was this translation helpful? Give feedback.
All reactions
-
🚀 1
-
Done
Beta Was this translation helpful? Give feedback.
All reactions
-
Done
Beta Was this translation helpful? Give feedback.
All reactions
-
Done!在很多语言都有语句和表达式的概念!
Beta Was this translation helpful? Give feedback.
All reactions
-
done
Beta Was this translation helpful? Give feedback.
All reactions
-
assert_eq!((), {});为什么是可以通过断言的呢?
亲测,在第一题的assert_eq!里把"3"改成"{}"或者"()"都是可以的。
Beta Was this translation helpful? Give feedback.
All reactions
-
assert_eq!() 的第二个参数是 {} ,是一个块作用域(也就是表达式),但是没有返回值,因此隐式地返回 ()
Beta Was this translation helpful? Give feedback.
All reactions
-
👍 19 -
❤️ 1
-
回答的好!
Beta Was this translation helpful? Give feedback.
All reactions
-
Done
Beta Was this translation helpful? Give feedback.
All reactions
-
x += 2
相当于
x = x + 2
虽然没有加 ;
但跑不起来,改成 x + 2 就行了,有点迷糊
没有加 ;
赋值操作也不算表达式么
fn main() {
let v = {
let mut x = 1;
x + 2
};
assert_eq!(v, 3);
}
Beta Was this translation helpful? Give feedback.
All reactions
-
我的理解是 x += 2 和 x = x + 2 一样都是语句,而 x + 2 是表达式,语句块用不带分号的表达式结尾才会返回一个数值,用不带分号的语句和带分号的语句结尾在这里都一样,返回"()"
Beta Was this translation helpful? Give feedback.
All reactions
-
以上理解似乎是错误的,x+=2好像也是表达式...
Beta Was this translation helpful? Give feedback.
All reactions
-
👍 2
-
是的,我的理解是,x+=2是表达式,没有返回值,所以{}返回默认()
Beta Was this translation helpful? Give feedback.
All reactions
-
我也没懂,为啥x+=2就不行 x+2就行
想了想,感觉是x += 2已经返回赋值给了x然后v就不知道给什么了,x+2是出来了3然后就返回赋值给v
Beta Was this translation helpful? Give feedback.
All reactions
-
总之,能返回值,它就是表达式
形似 x = 1
这种操作返回的是单元类型, 即是没有返回值的, 可以把它看做是表达式, 但结果就是返回单元类型 ()
, 为了区分语句和表达式, 也是为了优化性能(?存疑), 采用这种区分方式。
x += 2 和 x = x + 2 都是对右式进行求值, 在赋值左式, 最后的操作是赋值操作, 赋值操作是没有返回值的, 是一个语句, 如何把它看做表达式, 返回 ()
本人的简单理解, 如有不对, 请指正
Beta Was this translation helpful? Give feedback.
All reactions
-
👍 1
-
fn main() {
let v = {
let mut x = 1;
x += 2
};
assert_eq!(v, 3);
}
x=x+2这一句,按直觉理解应该是作为语句的,作为语句时应该加;则返回()。
但如果不加;,即是作为表达式,那么赋值语句的表达式可以返回(),我的理解对吗
Beta Was this translation helpful? Give feedback.
All reactions
-
不是的,语句是没有返回值的,在the book里面有这样的一段:
Statements are instructions that perform some action and do not return a value.
我认为应该是如果加 ;
的话就是语句,是没有返回值的。而如果不加 ;
的话,就是一个表达式,它有返回值,但是赋值表达式的返回值是 ()
Beta Was this translation helpful? Give feedback.
All reactions
-
👍 2
-
fn main() {
let a = 8;
}
和
fn main() {
let a = 8
}
都是对的,说明rust 在只有一行语句的时候可以不写";" 但是下面有一行就会报错,所以不加";"的有可能是语句
Beta Was this translation helpful? Give feedback.
All reactions
-
试了几种写法,觉得关键在mut上,因为mut是修改同一个内存地址上的值,并不会发生内存对象的再分配,所以最后x+=2才能作为表达式。但还是没有找到表达式和语句的本质区别相关的解释内容。
Beta Was this translation helpful? Give feedback.
All reactions
-
done
Beta Was this translation helpful? Give feedback.
All reactions
-
试了几种写法,觉得关键在mut上,因为mut是修改同一个内存地址上的值,并不会发生内存对象的再分配,所以最后x+=2才能作为表达式。但还是没有找到表达式和语句的本质区别相关的解释内容。
Beta Was this translation helpful? Give feedback.
All reactions
-
不好意思,我是想回复上面一条,发现自己回复错了
Beta Was this translation helpful? Give feedback.
All reactions
-
2022年11月9日 Done
Beta Was this translation helpful? Give feedback.
All reactions
-
Done!
Beta Was this translation helpful? Give feedback.
All reactions
-
Done
Beta Was this translation helpful? Give feedback.
All reactions
-
Done
Beta Was this translation helpful? Give feedback.
All reactions
-
mark
Beta Was this translation helpful? Give feedback.
All reactions
-
细节太多..
Beta Was this translation helpful? Give feedback.
All reactions
-
done
Beta Was this translation helpful? Give feedback.
All reactions
-
谢谢大佬的教程
Beta Was this translation helpful? Give feedback.
All reactions
-
mark
Beta Was this translation helpful? Give feedback.
All reactions
-
done
Beta Was this translation helpful? Give feedback.
All reactions
-
done!
Beta Was this translation helpful? Give feedback.
All reactions
-
day1
Beta Was this translation helpful? Give feedback.
All reactions
-
111
Beta Was this translation helpful? Give feedback.
All reactions
-
Done!
Beta Was this translation helpful? Give feedback.
All reactions
-
done
Beta Was this translation helpful? Give feedback.
All reactions
-
done
Beta Was this translation helpful? Give feedback.
All reactions
-
Done
Beta Was this translation helpful? Give feedback.
All reactions
-
第三题提供另一种解法:
fn main() { let v = {let x = 3;}; assert!(v == ()); }
Beta Was this translation helpful? Give feedback.
All reactions
-
✅
Beta Was this translation helpful? Give feedback.
All reactions
-
Success(3/3)
Beta Was this translation helpful? Give feedback.
All reactions
-
done
Beta Was this translation helpful? Give feedback.
All reactions
-
其实3也可以有两种方法解决
Beta Was this translation helpful? Give feedback.
All reactions
-
I'm the best
Beta Was this translation helpful? Give feedback.