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];.
相关推荐
PC2005-cloud19 分钟前
DSH 白嫖指南:接入 Command Code Go、WorkBuddy 与 Trae 的免费额度
开发语言·后端·golang
千里码aicood38 分钟前
基于Python语言的测量程序设计
开发语言·python
charlie11451419142 分钟前
C++ 的新标准容器:flat_map、inplace_vector 与 mdspan
开发语言·c++·开源项目
vx-程序开发43 分钟前
【计算机毕设】基于Spring Boot的古城景区管理系统88564
java·数据库·spring boot·后端·spring·elasticsearch·课程设计
唠玖馆1 小时前
c++AVL树
开发语言·c++
2401_858286111 小时前
130.【C语言】可变参数宏
c语言·开发语言
妙码生花1 小时前
两个月 59 篇 AI 开发日志 + Golang 商业级实战项目收工后,得来的 AI 使用心法-上
前端·后端·gin
霸道流氓气质1 小时前
通义千问 VL & Audio:图像、视频、语音多模态 Java 集成深度解析
java·开发语言·音视频
jayson.h1 小时前
python-可视化提取表格-通用
开发语言·python
vx-Biye_Design1 小时前
springboot中国传统节日宣传平台49078-计算机课程设计、毕业设计
java·前端·vue.js·spring boot·后端·课程设计·idea