Rust变量绑定-可变性

变量绑定在默认情况下是不可变的, 但是这可以使用 mut 修辞符来覆盖。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
fn main() {
let _immutable_binding = 1;
let mut mutable_binding = 1;
println!("Before mutation: {}", mutable_binding);
// Ok
mutable_binding += 1;
println!("After mutation: {}", mutable_binding);
// Error!
_immutable_binding += 1;
// FIXME ^ Comment out this line
}
编译器将抛出一个详细的诊断有关的可变性的错误。 

联系我们

邮箱 626512443@qq.com
电话 18611320371(微信)
QQ群 235681453

Copyright © 2015-2024

备案号:京ICP备15003423号-3