Compile error when trying to use increment operator

后端 未结 1 1412
粉色の甜心
粉色の甜心 2021-01-12 00:21

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++;
}

a

相关标签:
1条回答
  • 2021-01-12 00:43

    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.

    0 讨论(0)
提交回复
热议问题