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];.
相关推荐
Chen--Xing几秒前
LeetCode 49.字母异位词分组
c++·python·算法·leetcode·rust
柯南二号几秒前
【后端】【Java】RESTful书面应该如何写
java·开发语言·restful
切糕师学AI1 分钟前
如何用 VS Code + C# Dev Kit 创建类库项目并在主项目中引用它?
开发语言·c#
JIngJaneIL4 分钟前
基于Java+ vueOA工程项目管理系统(源码+数据库+文档)
java·开发语言·前端·数据库·vue.js·spring boot·后端
tang&8 分钟前
Qt 基础教程:从初识到信号槽机制
开发语言·qt
蓝鲸屿9 分钟前
JS基础第九天——对象(2)+Random
开发语言·前端·javascript
李绍熹22 分钟前
C语言数组与指针示例
c语言·开发语言
William数据分析22 分钟前
JavaScript 语法零基础入门:从变量到异步(附 Python 语法对比)
开发语言·javascript·python
爱学习的小可爱卢26 分钟前
JavaEE进阶——SpringBoot拦截器详解:从入门到实战
java·spring boot·后端
coderxiaohan27 分钟前
【C++】无序容器unordered_set和unordered_map的使用
开发语言·c++