feat: let mut 改为 var 关键字声明可变变量

This commit is contained in:
2026-06-05 19:47:00 +08:00
parent a28d33854c
commit ab4cc9a28b
10 changed files with 27 additions and 28 deletions
+12 -12
View File
@@ -83,19 +83,19 @@ let flag = true; // 类型推断为 bool
### 可变变量
```rust
let mut count: i64 = 0; // 可变变量
var count: i64 = 0; // 可变变量
count = count + 1; // 赋值
count += 1; // 复合赋值 (等价于 count = count + 1)
```
> **规则**: 对 `let` 声明的不可变变量赋值会在编译时报错。
> **规则**: 对 `let` 声明的不可变变量赋值会在编译时报错。可变变量用 `var` 声明。
### 类型标注语法
```rust
let name: Type = value; // 完整写法
let name = value; // 类型推断
let mut name: Type = value; // 可变变量
let name: Type = value; // 完整写法(不可变)
let name = value; // 类型推断(不可变)
var name: Type = value; // 可变变量
```
---
@@ -177,7 +177,7 @@ if x > 0 {
### while 循环
```rust
let mut i: i64 = 0;
var i: i64 = 0;
while i < 5 {
print_i64(i);
i += 1;
@@ -194,7 +194,7 @@ for i in 0 to 5 {
// 输出: 0 1 2 3 4
```
`for i in start to end` 等价于 `let mut i = start; while i < end { ...; i += 1; }`
`for i in start to end` 等价于 `var i = start; while i < end { ...; i += 1; }`
### match
@@ -373,8 +373,8 @@ print_i64(arr[1]); // 200
### 循环遍历
```rust
let mut arr: [i64; 5] = arr;
let mut i: i64 = 0;
var arr: [i64; 5] = arr;
var i: i64 = 0;
while i < 5 {
arr[i] = i * 10;
i += 1;
@@ -499,10 +499,10 @@ fn main() -> i64 {
```rust
fn main() -> i64 {
let mut arr: [i64; 5] = arr;
var arr: [i64; 5] = arr;
// 填充数组
let mut i: i64 = 0;
var i: i64 = 0;
while i < 5 {
arr[i] = i * i;
i += 1;
@@ -564,7 +564,7 @@ l_lang.exe source.l --emit-ir
```
类型: i64 f64 bool str void struct enum [T;N]
声明: let x = val let mut x = val
声明: let x = val var x = val
let x: Type = val type Alias = Type
控制流: if/else while for i in 0 to N match
函数: fn name(p: T) -> Ret { return expr; }