gpt4 book ai didi

rust - 尝试使用增量运算符时编译错误

转载 作者:行者123 更新时间:2023-11-29 07:51:53 25 4
gpt4 key购买 nike

在业余项目的工作中,我尝试使用增量运算符,如下所示:

fn main() {
let mut my_var = 5;
my_var++;
}

并收到以下错误:

error: expected expression, found `+`
--> src\main.rs:3:12
|
3 | my_var++;
| ^

我的代码有什么问题?

最佳答案

递增 (++) 和递减 (--) 运算符在 Rust 中不支持

来自 Rust's FAQ :

Why doesn't Rust have increment and decrement operators?
Preincrement and postincrement (and the decrement equivalents), while convenient, are also fairly complex. They require knowledge of evaluation order, and often lead to subtle bugs and undefined behavior in C and C++. x = x + 1 or x += 1 is only slightly longer, but unambiguous.

关于rust - 尝试使用增量运算符时编译错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40073785/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com