学习rust中的变量,常量定义,包括类型int float 等,看具体代码:

fn main() {
    println!("Hello, world!");
    // 定义一个整数变量 temp_int,类型为 i16 ,初始值为 10,不可变更
    // i16 类型的整数范围为 -32768 到 32767,占用 2 字节内存,有符号 16位二进制int, 类似的还有
    // i8, u8, i16, u16, i32(默认), u32, i64, u64(默认) ,f32, f64, isize, usize
    // 其中,isize 和 usize 是平台相关的类型,分别表示当前平台的有符号整数和无符号整数
    let temp_int: i16 = 10;
    println!("temp_int = {}", temp_int);
    // 可变变量
    let mut temp_int_mut: i16 = 10;
    println!("temp_int_mut = {}", temp_int_mut);
    temp_int_mut = 20;
    println!("temp_int_mut = {}", temp_int_mut);
    //求和
    temp_int_mut += temp_int;
    println!("temp_int_mut_add = {}", temp_int_mut);

    // 定义浮点数变量 temp_float,类型为 f32 ,初始值为 3.14
    let temp_float: f32 = 3.14;
    println!("temp_float = {}", temp_float);
    // 可以二次定义 重复的名称
    let temp_int: i16 = 10;
    println!("redifine temp_int = {}", temp_int);

    // 定义字符串变量 temp_str,类型为 &str ,初始值为 "hello",不可变化
    let temp_str: &str = "hello";
    println!("temp_str = {}", temp_str);
    // 字符串链接,将 temp_str 和 temp_str2 连接起来,结果为 temp_str3,类型为 String 类型
    let temp_str2: &str = "world";

    // 可变字符串
    let mut temp_str3: String = temp_str.to_string() + temp_str2;
    println!("temp_str3 = {}", temp_str3);
    // (1) 追加单个字符
    temp_str3.push('!');
    // (2) 追加字符串切片
    temp_str3.push_str(" 你好");
    // (3) 打印 temp_str3
    println!("temp_str3 = {}", temp_str3);

    // 常量使用 `const` 声明,必须标注类型
    // 常量在整个程序运行期间都存在
    const MAX_POINTS: u32 = 100_000; // 数字字面量可以添加下划线提高可读性
    println!("最大点数 = {}", MAX_POINTS);
}