Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

basic-types/statements-expressions #183

giscus[bot] bot announced in Book Comments
Mar 29, 2022 · 80 comments · 22 replies
Discussion options

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

You must be logged in to vote

Replies: 80 comments 22 replies

Comment options

完成 3/3

You must be logged in to vote
1 reply
Comment options

nice practice!

Comment options

done

You must be logged in to vote
2 replies
Comment options

Done

Comment options

Done

Comment options

Done!在很多语言都有语句和表达式的概念!

You must be logged in to vote
0 replies
Comment options

done

You must be logged in to vote
0 replies
Comment options

assert_eq!((), {});为什么是可以通过断言的呢?
亲测,在第一题的assert_eq!里把"3"改成"{}"或者"()"都是可以的。

You must be logged in to vote
2 replies
Comment options

assert_eq!() 的第二个参数是 {} ,是一个块作用域(也就是表达式),但是没有返回值,因此隐式地返回 ()

Comment options

回答的好!

Comment options

Done

You must be logged in to vote
0 replies
Comment options

x += 2
相当于
x = x + 2
虽然没有加 ; 但跑不起来,改成 x + 2 就行了,有点迷糊
没有加 ; 赋值操作也不算表达式么

fn main() {
 let v = {
 let mut x = 1;
 x + 2
 };
 assert_eq!(v, 3);
}
You must be logged in to vote
6 replies
Comment options

我的理解是 x += 2 和 x = x + 2 一样都是语句,而 x + 2 是表达式,语句块用不带分号的表达式结尾才会返回一个数值,用不带分号的语句和带分号的语句结尾在这里都一样,返回"()"

Comment options

以上理解似乎是错误的,x+=2好像也是表达式...

Comment options

是的,我的理解是,x+=2是表达式,没有返回值,所以{}返回默认()

Comment options

我也没懂,为啥x+=2就不行 x+2就行

想了想,感觉是x += 2已经返回赋值给了x然后v就不知道给什么了,x+2是出来了3然后就返回赋值给v

Comment options

总之,能返回值,它就是表达式

形似 x = 1 这种操作返回的是单元类型, 即是没有返回值的, 可以把它看做是表达式, 但结果就是返回单元类型 (), 为了区分语句和表达式, 也是为了优化性能(?存疑), 采用这种区分方式。

x += 2 和 x = x + 2 都是对右式进行求值, 在赋值左式, 最后的操作是赋值操作, 赋值操作是没有返回值的, 是一个语句, 如何把它看做表达式, 返回 ()

本人的简单理解, 如有不对, 请指正

Comment options

fn main() {
 let v = {
 let mut x = 1;
 x += 2
 };
 assert_eq!(v, 3);
}

x=x+2这一句,按直觉理解应该是作为语句的,作为语句时应该加;则返回()。
但如果不加;,即是作为表达式,那么赋值语句的表达式可以返回(),我的理解对吗

You must be logged in to vote
3 replies
Comment options

不是的,语句是没有返回值的,在the book里面有这样的一段:

Statements are instructions that perform some action and do not return a value.

我认为应该是如果加 ; 的话就是语句,是没有返回值的。而如果不加 ; 的话,就是一个表达式,它有返回值,但是赋值表达式的返回值是 ()

Comment options

fn main() {
let a = 8;
}


fn main() {
let a = 8
}
都是对的,说明rust 在只有一行语句的时候可以不写";" 但是下面有一行就会报错,所以不加";"的有可能是语句

Comment options

试了几种写法,觉得关键在mut上,因为mut是修改同一个内存地址上的值,并不会发生内存对象的再分配,所以最后x+=2才能作为表达式。但还是没有找到表达式和语句的本质区别相关的解释内容。

Comment options

done

You must be logged in to vote
2 replies
Comment options

试了几种写法,觉得关键在mut上,因为mut是修改同一个内存地址上的值,并不会发生内存对象的再分配,所以最后x+=2才能作为表达式。但还是没有找到表达式和语句的本质区别相关的解释内容。

Comment options

不好意思,我是想回复上面一条,发现自己回复错了

Comment options

2022年11月9日 Done

You must be logged in to vote
0 replies
Comment options

Done!

You must be logged in to vote
1 reply
Comment options

Done

Comment options

Done

You must be logged in to vote
0 replies
Comment options

mark

You must be logged in to vote
0 replies
Comment options

细节太多..

You must be logged in to vote
0 replies
Comment options

done

You must be logged in to vote
0 replies
Comment options

谢谢大佬的教程

You must be logged in to vote
0 replies
Comment options

mark

You must be logged in to vote
0 replies
Comment options

done

You must be logged in to vote
0 replies
Comment options

done!

You must be logged in to vote
0 replies
Comment options

day1

You must be logged in to vote
0 replies
Comment options

111

You must be logged in to vote
0 replies
Comment options

Done!

You must be logged in to vote
0 replies
Comment options

done

You must be logged in to vote
0 replies
Comment options

done

You must be logged in to vote
0 replies
Comment options

Done

You must be logged in to vote
0 replies
Comment options

第三题提供另一种解法:

fn main() {
 let v = {let x = 3;};
 assert!(v == ());
}
You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

Success(3/3)

You must be logged in to vote
0 replies
Comment options

done

You must be logged in to vote
0 replies
Comment options

其实3也可以有两种方法解决

You must be logged in to vote
1 reply
Comment options

I'm the best

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

AltStyle によって変換されたページ (->オリジナル) /