Compile error when trying to use increment operator

一笑奈何 提交于 2020-03-01 01:48:29

问题


During work on a side project I've tried to use an increment operator, as following:

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

and received the following error:

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

What's wrong with my code?


回答1:


Increment (++) and decrement (--) operators are not supported in Rust.

From 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.



来源:https://stackoverflow.com/questions/40073785/compile-error-when-trying-to-use-increment-operator

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!