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];.
相关推荐
在角落发呆几秒前
DTU 数据转发服务器:工业物联网的隐形桥梁
开发语言·php
神奇小汤圆3 分钟前
MySQL慢查询优化案例:真实案例+EXPLAIN分析——性能提升10倍!
后端
Sakuyu434684 分钟前
C语言基础--基本数据类型
c语言·开发语言
在坚持一下我可没意见6 分钟前
Python 修仙修炼录 05:循环神通,省去无用苦修
开发语言·python·面试·入门·循环·复习
还没学会摸鱼的钓鱼仔19 分钟前
手撕 LangChain Deep Agents 源码 (一):create_deep_agent 是如何"组装"出一个 AI 操作系统的
后端
用户2986985301420 分钟前
Java 操作 Word 文档:数学公式与符号的插入方法
java·后端
techdashen24 分钟前
Rust 社区在 4 月做了什么:项目管理月报解读
开发语言·rust·mfc
小撒的私房菜25 分钟前
Day 5:Agent Loop——整个系列里最关键的一天
人工智能·后端
十五年专注C++开发25 分钟前
QFluentKit: 一个基于 Qt Widgets 的 Fluent Design 风格 UI 组件库
开发语言·c++·qt·ui·qfluentkit
lly20240626 分钟前
PHP JSON 使用指南
开发语言