Day 3: 变量与基本数据类型
今天是《30天精通Rust语言》系列的第三天。我们将学习Rust中最基础但最重要的概念:变量声明、基本数据类型以及类型推断系统。扎实的语法基础是深入学习的前提。
2. 变量声明与不可变性
Section titled “2. 变量声明与不可变性”Rust中的变量默认是不可变的(immutable),这是Rust安全性的基石之一。
2.1 基本变量声明
Section titled “2.1 基本变量声明”fn main() { let x = 5; println!("The value of x is: {}", x);}在这个例子中,x 被声明并初始化为5。注意:
- 不需要显式声明类型,编译器会自动推断。
x是不可变的,尝试修改x = 6将导致编译错误。
2.2 可变变量
Section titled “2.2 可变变量”如果需要变量值可以改变,使用 mut 关键字:
fn main() { let mut x = 5; println!("The value of x is: {}", x);
x = 6; // 允许修改 println!("The value of x is: {}", x);}2.3 变量遮蔽(Shadowing)
Section titled “2.3 变量遮蔽(Shadowing)”Rust允许在同一个作用域内用 let 重新声明同名变量,这称为”变量遮蔽”:
fn main() { let x = 5; let x = x + 1; // 创建一个新的x,值加1 let x = x * 2; // 再创建一个新的x,值乘2 println!("The value of x is: {}", x); // 输出12}变量遮蔽与 mut 的区别:遮蔽创建了新变量,可以改变类型;mut 修改现有变量值,但不能改变类型。
3. 基本数据类型
Section titled “3. 基本数据类型”Rust是静态类型语言,每个值都有其类型。常用基本类型分为标量类型和复合类型。
3.1 整数类型
Section titled “3.1 整数类型”| 类型 | 描述 |
|---|---|
i8/u8 | 8位有符号/无符号整数 |
i16/u16 | 16位有符号/无符号整数 |
i32/u32 | 32位有符号/无符号整数(默认) |
i64/u64 | 64位有符号/无符号整数 |
isize/usize | 指针大小的有符号/无符号整数 |
fn main() { let a: i32 = 42; let b: u32 = 100; let c = -10; // 推断为i32 println!("a={}, b={}, c={}", a, b, c);}3.2 浮点数类型
Section titled “3.2 浮点数类型”Rust有两种浮点类型:f32(32位单精度)和 f64(64位双精度,默认)。
fn main() { let x: f64 = 3.14159; let y: f32 = 2.71828; println!("PI ≈ {}, e ≈ {}", x, y);}3.3 布尔类型
Section titled “3.3 布尔类型”fn main() { let is_rust_awesome: bool = true; let is_java_better = false;
if is_rust_awesome { println!("Rust is awesome!"); }}3.4 字符类型
Section titled “3.4 字符类型”Rust的 char 类型表示单个Unicode字符,用单引号包裹:
fn main() { let c1: char = 'A'; let c2 = '中'; let c3 = '😀';
println!("Characters: {}, {}, {}", c1, c2, c3);}注意:Rust的 char 是4字节,支持完整的Unicode,而不像C/C++的char仅支持ASCII。
4. 类型推断
Section titled “4. 类型推断”Rust拥有强大的类型推断系统,大多数情况下不需要显式声明类型:
fn main() { let x = 42; // 推断为i32 let y = 3.14; // 推断为f64 let z = true; // 推断为bool let name = "Rust"; // 推断为&str
println!("x={}, y={}, z={}, name={}", x, y, z, name);}5. 显式类型标注
Section titled “5. 显式类型标注”当类型推断无法确定或需要明确指定时,使用冒号 : 标注类型:
fn main() { let x: i64 = 1000000; let y: f32 = 3.14; let arr: [i32; 3] = [1, 2, 3]; // 数组类型 println!("x={}, y={}, arr={:?}", x, y, arr);}6. 数值运算
Section titled “6. 数值运算”Rust支持标准算术运算符:
fn main() { let sum = 5 + 10; let difference = 95.5 - 4.3; let product = 4 * 30; let quotient = 56.7 / 32.2; let remainder = 43 % 5;
println!("sum={}, diff={}, prod={}, quot={}, rem={}", sum, difference, product, quotient, remainder);}常量使用 const 关键字声明,必须标注类型,且值在编译时就确定:
const MAX_POINTS: u32 = 100_000;
fn main() { println!("Max points: {}", MAX_POINTS);}常量与不可变变量的区别:常量永远不可变,且必须显式标注类型。
8. 字符串类型初步
Section titled “8. 字符串类型初步”Rust有两种主要字符串类型:
- String:堆分配的动态字符串,有所有权。
- &str:字符串切片,通常作为引用使用。
fn main() { let s1: String = String::from("Hello"); let s2: &str = "World";
println!("{} {}!", s1, s2);}字符串的详细内容将在后续章节详细讨论。
9. 实践练习
Section titled “9. 实践练习”今天的实践任务:
- 声明一个不可变变量
age,赋值为你的年龄。 - 声明一个可变变量
score,初始值为0,每次运行将分数增加10。 - 使用变量遮蔽,将数字
7先转换为7.0,再转换为"seven"(字符串)。 - 创建一个计算圆面积的程序,已知半径
r = 5.0,输出面积。 - 编写程序验证各种整数和浮点数的取值范围。
10. 总结
Section titled “10. 总结”今天我们学习了Rust的变量声明、基本数据类型和类型推断系统。关键要点:
- 变量默认不可变,使用
mut使其可变。 - Rust有丰富的整数、浮点、布尔和字符类型。
- 强大的类型推断减少了显式类型标注的需要。
- 常量使用
const声明,必须标注类型。
明天我们将学习函数定义、参数传递和控制流语句。加油!