变量定义
通过let引入变量,rust变量特征(摘自ref1):
- Immutable by default: Variables in Rust cannot be changed once assigned, unless explicitly declared as mutable
- Static typing: Every variable has a defined type at compile time
- Type inference: Rust can often figure out the type for you
- Shadowing: You can declare a new variable with the same name as a previous variable
常量
常量1必须通过constant关键字引入,且必须标注类型,必须初始化(编译期可计算)。
摘自ref1,
Besides variables, Rust also has constants. Constants are values that are bound to a name and cannot change, like immutable variables. However, there are a few differences:
- You must annotate the type
- Constants use the
constkeyword instead oflet - Constants can be declared in any scope, including the global scope
- Constants can only be set to a constant expression, not a function call result or anything computed at runtime
fn main() {
const MAX_POINTS: u32 = 100_000; // 必须显式标注常量类型
println!("The maximum points is: {}", MAX_POINTS);
}Rust类型
Scalar Types(标量类型)
Integers(整型)
| Length | Signed | Unsigned |
|---|---|---|
| 8-bit | i8 | u8 |
| 16-bit | i16 | u16 |
| 32-bit | i32 | u32 |
| 64-bit | i64 | u64 |
| 128-bit | i128 | u128 |
| arch | isize | usize |
- Signed integers can represent both positive and negative numbers
- Unsigned integers can only represent positive numbers
- The
isizeandusizetypes depend on the architecture of the computer (64 bits on 64-bit architecture)
// Integer type examples
let a: i32 = -42; // 32-bit signed integer
let b: u8 = 255; // 8-bit unsigned integer
let c = 100_000; // Type inferred as i32 by default
let d = 0xff; // Hexadecimal
let e = 0o77; // Octal
let f = 0b1111_0000; // Binary with visual separator
let g: usize = 123; // Architecture-dependent sizeFloat(浮点数)
// Floating-point examples
let x: f64 = 2.0; // 64-bit float (double precision) - double in c++
let y: f32 = 3.14; // 32-bit float (single precision)
let z = 10.5; // Type inferred as f64 by defaultBoolean(布尔)
// Boolean examples
let is_active: bool = true;
let is_greater = 10 > 5; // Evaluates to trueCharacters(字符)
The char type represents a Unicode Scalar Value, which means it can represent more than just ASCII.
// Character examples
let letter: char = 'A'; // Single quotes for char literals
let emoji = '😊'; // Supports Unicode characters
let kanji = '日'; // Supports international characters所以rust的char并非1个字节,而是4个字节(通常)。
Compound Types(复合类型)
两个主要复合类型,
- tuple:将多个类型组合的变量组合在一起
- array:将同类型变量组合在一起
String Types(字符串)
String Slices (&str)
A string slice is a reference to a part of a string and is commonly seen as string literals.
// String slice examples
let greeting: &str = "Hello, world!";
let part: &str = &greeting[0..5]; // Takes a slice of the greeting (Hello)
println!("Part: {}", part);
// Output: Part: HelloString Type
The String type is a growable, mutable, owned UTF-8 encoded string.
// String examples
let mut s = String::from("Hello"); // Creates a String from a string literal
s.push_str(", world!"); // Appends a string slice
println!("{}", s);
// Output: Hello, world!
// Converting between String and &str
let slice: &str = &s; // &String coerces to &str
let owned: String = slice.to_string(); // Convert &str to StringCollection Types(容器类型)
- Vec
- HashMap
Custom Types
- struct
- enum
References
Footnotes
-
Rust常量一般用全大写,下划线连接。 ↩