Rust Learning Day2

Data Type

rust 复制代码
fn main() {
    // Scalar data types: integers, floats, characters, booleans
    // Default integer type is i32 (32-bit signed)
    // Default float type is f64 (64-bit)

    // Integers
    let x = 42; // i32 by default
    let y: i64 = 123456789; // Explicitly specifying i64
    println!("Integer x: {x}, Integer y: {y}");

    // Floats
    let z = 3.14; // f64 by default
    let w: f32 = 2.718; // Explicitly specifying f32
    println!("Float z: {z}, Float w: {w}");

    // Characters: use single quotes, and they represent Unicode scalar values
    let c = 'z';
    let heart_eyed_cat = '😻';
    println!("Character c: {c}, Emoji: {heart_eyed_cat}");

    // Booleans
    let t = true;
    let f: bool = false; // Explicitly specifying the type
    println!("Boolean t: {t}, Boolean f: {f}");

    // Compound data types: tuples and arrays
    // Tuples: fixed size, can contain elements of different types

    // Two ways to access tuple elements
    let tup: (i32, f64, i32) = (400, 6.5, 1);

    // Destructuring the tuple
    let (_, y, _) = tup;
    println!("The value of y in tuple is {y}");

    // Accessing tuple elements by index
    let first = tup.0;
    println!("The first value of tup is {first}");

    // Arrays: fixed size, all elements are of the same type
    // `a` contains five i32 elements
    let a = [1, 2, 3, 4, 5];

    // `b` contains six elements, all with the value 3
    let b = [3; 6];

    // Accessing array elements is similar to other programming languages
    let first = a[0];
    println!("The first value of a is {first}");
}

Explanation:

  1. Scalar Data Types:

    • Integers:
      • Default type is i32, which is a 32-bit signed integer.
      • Example of explicit type declaration: let y: i64 = 123456789;.
    • Floats:
      • Default type is f64, which is a 64-bit floating-point number.
      • Example of explicit type declaration: let w: f32 = 2.718;.
    • Characters:
      • Characters are specified using single quotes and represent Unicode scalar values.
      • Example: let c = 'z'; and let heart_eyed_cat = '😻';.
    • Booleans:
      • Boolean values can be true or false.
      • Example: let t = true; and let f: bool = false;.
  2. Compound Data Types:

    • Tuples:
      • Tuples have a fixed size and can contain elements of different types.
      • You can access elements by destructuring: let (_, y, _) = tup;.
      • Or by indexing: let first = tup.0;.
    • Arrays:
      • Arrays have a fixed size and all elements must be of the same type.
      • You can initialize arrays with specific values: let a = [1, 2, 3, 4, 5];.
      • Or with a repeated value: let b = [3; 6];.
相关推荐
葫芦和十三1 小时前
图解 MongoDB 02|BSON:你以为存的是 JSON,其实是带类型的二进制
后端·mongodb·agent
葫芦和十三1 小时前
图解 MongoDB 01|文档数据库
后端·mongodb·agent
陈随易3 小时前
VSCode的Copilot扩展支持接入DeepSeek,Kimi了!
前端·后端·程序员
我不是外星人5 小时前
有了 Harness Engineering ,真的还需要研发工程师吗?
前端·后端·ai编程
candyTong5 小时前
RTK 技术原理:一次典型会话里,80% 上下文是怎么省下来的
javascript·后端·架构
Rust研习社7 小时前
组合真的优于继承吗?为什么 Rust 和 Go 都拥抱组合舍弃继承?
后端·rust·编程语言
IT_陈寒7 小时前
JavaScript的闭包把我坑惨了,说好的内存会自动回收呢?
前端·人工智能·后端
CaffeinePro8 小时前
Pydantic深度使用:数据校验、枚举、ORM映射
后端·fastapi
Chenyiax9 小时前
从 Chat 到 Responses:OpenAI API 抽象为什么变了?
后端
MariaH9 小时前
Koa和Express的区别
后端