学习Rust的第4天:常见编程概念

news2025/6/8 19:41:19

欢迎来到学习Rust的第四天,基于Steve Klabnik的《The Rust Programming Language》一书。昨天我们做了一个 猜谜游戏 ,今天我们将探讨常见的编程概念,例如:

  • Variables 变量
  • Constants 常数
  • Shadowing 阴影
  • Data Types 数据类型
  • Functions 功能

So let’s just dive in?
所以我们就开始吧?

Variables 变量

In layman terms, Variables are like container that store a value. Let’s just take a quick look at them
通俗地说,变量就像存储值的容器。让我们快速的看一下

  • Variables can store different types of values such as : numbers, text or complex structures
    变量可以存储不同类型的值,例如:数字、文本或复杂结构
  • Each variable has a name
    每个变量都有一个名称
  • In rust a variable is immutable by default, meaning the value cannot be change once set, to make a variable mutable mut keyword is used.
    在rust中,变量默认是不可变的,这意味着一旦设置了值就不能改变,要使变量可变,使用 mut 关键字。
  • You can explicitly define what type of value is going to be stored in a variable but it is optional.
    你可以显式定义什么类型的值将被存储在变量中,但它是可选的。
  • Variables have a limited scope, meaning they can only be accessed in a certain portion of the code
    变量的作用域有限,这意味着只能在代码的某个部分访问它们

Example : 范例:

fn main(){
  let x = 10; //immutable var
  let mut y = 15; //mutable var
  println!("X = {}",x);
  println!("Y = {}",y);

  y = 2
  println!("X = {}",x);
  println!("Y = {}",y);
}

Constants 常数

Like immutable variables, constants are values that are not allowed to change, but there are a few differences
与不可变变量一样,常量是不允许更改的值,但也有一些不同之处

  • Constants are inherently immutable, mut keyword cannot be used with them
    常量本质上是不可变的, mut 关键字不能与它们一起使用
  • They are initialized at the time of compilation, compile-time initialization
    它们在编译时初始化,编译时初始化
  • Constants have a global scope and can be accessed from anywhere in the program
    常量具有全局作用域,可以从程序中的任何位置访问
  • Type definition is required for constants
    常量需要类型定义

Example : 范例:

const VALUE_OF_PI: u32 = 3.1418;

Shadowing 阴影

As we saw yesterday, with shadowing you can declare a new variable with the same name as the previous variable and the newer variable will overshadow the previous one
正如我们昨天所看到的,使用阴影,您可以声明一个与前一个变量同名的新变量,并且新变量将覆盖前一个变量

Example : 范例:

fn main(){
  let x = 10;
  let x = x - 7;
  {
    let x = x * 2;
    println!("X in inner scope = {}",x);
  }
  println!("X in oouter scope = {}",x);
}

Output : 输出量:

$ cargo run
X in inner scope = 6
X in outer scope = 3

Data Types 数据类型

Data types can further be divided into two groups: Scalar and Compound data types
数据类型可以进一步分为两组:标量和复合数据类型

Scalar Data Types 标量数据类型

A scalar type represents one value, Rust has 4 primary Scalar data types:
标量类型代表一个值,Rust有4种主要的标量数据类型:

Integer 整数

An integer is a number without the fractional component, We used it yesterday in the guessing game, u32 means an unsigned 32 bit integer
整数是一个没有小数部分的数字,我们昨天在猜谜游戏中使用了它, u32 表示无符号的32位整数

Other types of integer include :
其他类型的整数包括:

Signed : 签署人:

  • The difference between a signed and an unsigned integer is whether the number needs to have a sign or whether it will only be positive
    有符号整数和无符号整数之间的区别在于数字是否需要有一个 sign ,或者它是否只会是正数
  • Each signed integer can store numbers from -(2^(n — 1)) to (2^(n — 1)) — 1, Where n = the number of bits the variant uses
    每个有符号整数可以存储从-(2^(n - 1))到(2^(n - 1))- 1的数字,其中n =变体使用的位数
  • Example : i8, i16, i32, i64, i128
    示例:i8、i16、i32、i64、i128

Unsigned : 未签名:

  • Unsigned numbers are always positive.
    无符号数总是正数。
  • Each unsigned integer can store numbers from 0 to 2^n — 1
    每个无符号整数可以存储从0到 2^n — 1 的数字
  • Example : u8, u16, u32, u64, u128
    示例:u8、u16、u32、u64、u128

Additionally there is isize and usize depend on the architecture of the computer the program is running on. 32 bits if the program is running on 32 bit architecture and 64 bits if the program is running on 64 bit architecture
此外,还有 isize 和 usize 取决于程序运行的计算机的架构。如果程序运行在32位架构上,则为32位,如果程序运行在64位架构上,则为64位

Integer Overflow: 溢出:

  • Computers represent integers using a fixed number of bits, determining the range they can cover.
    计算机使用固定的位数来表示整数,确定它们可以覆盖的范围。
  • In Rust, when the result of an arithmetic operation exceeds the maximum representable value for a data type (overflow), it wraps around to the minimum value and continues counting.
    在Rust中,当算术运算的结果超过数据类型的最大可表示值(溢出)时,它会返回到最小值并继续计数。

Example of Integer overflow:
内存溢出示例:

If you’re using an 8-bit integer, the range is 0 to 255. If you try to add 1 to 255, in normal arithmetic, you’d expect 256. However, in Rust, it wraps around to 0 due to overflow.
如果使用8位整数,则范围为0到255。如果你试着把1加到255,在正常的算术中,你会得到256。然而,在Rust中,由于溢出,它会返回到0。

Floating-point numbers : 浮点数:

Floating point numbers in rust have a fractional component. Unlike integers Floating-point numbers are always signed. There are two types of floating-point numbers : f32 and f64 32 and 64 representing the size in bits.
rust中的浮点数有一个小数部分。与整数不同,浮点数总是有符号的。有两种类型的浮点数: f32 和 f64 32和64表示位的大小。

Example : 范例:

fn main() {
    // Using f32
    let float_32: f32 = 3.14; // A 32-bit floating-point number
    println!("f32 value: {}", float_32);

    // Using f64
    let float_64: f64 = 3.141592653589793; // A 64-bit floating-point number
    println!("f64 value: {}", float_64);

    // Performing arithmetic operations
    let result_f32 = float_32 * 2.0;
    let result_f64 = float_64 * 2.0;

    // Displaying results
    println!("Result (f32): {}", result_f32);
    println!("Result (f64): {}", result_f64);
}

Numeric operations 数值运算

Rust supports the basic mathematical operations such as :
Rust支持基本的数学运算,例如:

  • Addition 此外
  • Subtraction 减法
  • Multiplication 乘法
  • Division 司
  • Modulus 模量

Example: 范例:

fn main(){
  let sum = 10+7;
  let subtraction = 60-4;
  let multilpication = 7 * 7;
  let division = 25/5;
  let modulus = 13%2;
  
  println!("10 + 7 = {}",sum);
  println!("60 - 4 = {}",subtraction);
  println!("7 * 7 = {}",multilpication);
  println!("25 / 5= {}",division);
  println!("13 % 2= {}",modulus);
}

Boolean Type 布尔类型

As in most programming languages, a boolean type is one byte in size and can be either true or false.
与大多数编程语言一样,布尔类型的大小是一个字节,可以是 true 或 false 。

Example: 范例:

fn main(){
  let t = true;
  let f: bool = false;
}

Character type 字符类型

Rust’s char type is the most primitive alphabetic type, similar to char in C.
Rust的char类型是最原始的字母类型,类似于C中的 char 。

fn main(){
  let z = 'Z';
  let x: char = "X";
}

Side note : Characters are enclosed in single quotes, and Strings are enclosed in double quotes.
旁注:字符用单引号括起来,字符串用双引号括起来。

Compound Types 复合类型

Compound types can store multiple values in one type. They are often group of scalar data.
复合类型可以在一个类型中存储多个值。它们通常是一组标量数据。

Tuple 元组

A tuple in Rust is like a mixed bag that can hold different types of items, allowing you to group and organize them in a specific order. It’s a simple way to bundle multiple values together.
Rust中的元组就像一个混合的袋子,可以容纳不同类型的项目,允许您以特定的顺序对它们进行分组和组织。这是一种将多个值捆绑在一起的简单方法。

Tuples are immutable. However, the elements within the tuple may be mutable if they are declared as mutable variables.
元组不可变。但是,如果元组中的元素被声明为可变变量,则它们可能是可变的。

Syntax 语法

let var_name: (data_type1,data_type2...data_typeN) = (val1,val2...valN);

//The values in this tuple is mutable
let mut var_name: (data_type1,data_type2...data_typeN) = (val1,val2...valN);

Example 例如

let tup: (i32, f64, u16) = (500,3.141,9);

Accessing data in tuples 将数据存储在元组中

Indexing: 索引:

Indexing is the act of accessing a specific element in a data structure, like an array or tuple, using its position or key.
索引是访问数据结构中特定元素的行为,如数组或元组,使用其位置或键。

To access a tuple using indexing we can do this:
要使用索引访问元组,我们可以这样做:

println!("{}",tuple_name.index);

Example: 范例:

fn main() {
    // Creating a tuple
    let my_tuple = (42, "hello", 3.14);

    // Accessing elements using indexing (zero-based)
    let first_element = my_tuple.0;   // Access the first element (42)
    let second_element = my_tuple.1;  // Access the second element ("hello")
    let third_element = my_tuple.2;   // Access the third element (3.14)

    // Printing the accessed elements
    println!("First element: {}", first_element);
    println!("Second element: {}", second_element);
    println!("Third element: {}", third_element);
}

Array 阵列

Arrays are another way to store data of the same type, sequentially. Arrays in rust have a fixed length.
数组是按顺序存储相同类型数据的另一种方式。rust中的数组有固定的长度。

To write an array we use square brackets [] as a comma-seperated list.
为了写一个数组,我们使用方括号 [] 作为逗号分隔的列表。

Example: 范例:

fn main(){
  let a: [i32;5]= [1,2,3,4,5];
}

We can also do this :
我们也可以这样做:

fn main(){
  let x = [6; 4]; // [6,6,6,6]
}

Accessing array elements:
数组元素:

fn main() {
    let a: [u32; 6] = [1, 2, 3, 4, 5, 6];
    let first: u32 = a[0];
    let last: u32 = a[a.len() - 1];

    println!("First element : {}", first); // 1
    println!("Last element : {}", last); // 6
}

Functions 功能

Technically the definition of a functions is, a self-contained unit of code that performs a specific task, taking inputs and producing outputs, enhancing code structure and reusability.
从技术上讲,函数的定义是一个独立的代码单元,它执行特定的任务,接受输入并产生输出,增强代码结构和可重用性。

The main function is the entry point of a rust porgram, similarly we can use the fn keyword to declare custom functions that perform a specific task.
main 函数是rust程序的入口点,同样,我们可以使用 fn 关键字来声明执行特定任务的自定义函数。

In rust snake_case is the convention for declaring functions and variable names.
在rust中, snake_case 是声明函数和变量名的约定。

Example 例如

fn main(){
  println!("The main Function");
  new_function();
}

fn new_function(){
  println!("This is a custom function");
}

Output 输出

$ cargo run
The main Function
This is a custom function

Parameters 参数

In layman’s terms : A parameter is an input passed to a function
通俗地说:参数是传递给函数的输入

Technically, Parameters are a function’s placeholders in its definition and arguments are the actual values or data you provide to a function when you call it. They match the parameters’ types and order defined in the function.
从技术上讲,参数是函数定义中的占位符,参数是调用函数时提供给函数的实际值或数据。它们与函数中定义的参数类型和顺序相匹配。

Although people do tend to use these words interchangeably.
尽管人们倾向于互换使用这些词。

Example 例如

fn main(){
    println!("The main Function");
    addition(5,9);
}
fn addition(x: i32,y: i32){
    println!("Addition of {} + {} = {}",x,y,x+y);
}

Output 输出

$ cargo run
The main Function
Addition of 5 + 9 = 14

Statements and expressions
语句和表达式

Statements are instruction that perform a certain task, Whereas Expressions evaluate to a value.
语句是执行特定任务的指令,而表达式的计算结果是一个值。

Example 例如

fn main(){
  let y = 6; //Statement
  let x = 5; //Statement

  let result = x+y; // here "x+y" is an expression
}

Functions with return values
返回值函数

Functions can return a value when they are called . We must declare the return type of a function with an arrow ->
函数在被调用时可以返回一个值。我们必须用箭头声明函数的返回类型 ->

In rust, The final expression of the function is the return value of the function.
在rust中,函数的最终表达式是函数的返回值。

Example 例如

fn main(){
    println!("The main Function");
    let sum = addition(5,9);
    println!("Result of the function : {}", sum);
}
fn addition(x: i32,y: i32) -> i32 {
    x+y // The return statement
}

In Rust, the absence of a semicolon at the end of a return statement indicates that it is the value to be returned, making the code more explicit and reducing redundancy.
在Rust中,return语句末尾没有一个numberon表示它是要返回的值,使代码更加明确并减少冗余。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1592580.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

错题记录(2)

来源: FPGA开发/数字IC笔试系列(10) 笔试刷题 笔试 | 海思2022数字IC模拟卷(真题模拟,带解析) 运算符优先级

【C++算法竞赛 · 图论】图的存储

前言 图的存储 邻接矩阵 方法 复杂度 应用 例题 题解 邻接表 方法 复杂度 应用 前言 上一篇文章中(【C算法竞赛 图论】图论基础),介绍了图论相关的概念和一种图的存储的方法,这篇文章将会介绍剩下的两种方法&#xff…

【黑马头条】-day09用户行为-精度丢失-点赞收藏关注

文章目录 1 long类型精度丢失问题1.1 解决1.2 导入jackson序列化工具1.3 自定义注解1.4 原理1.5 测试 2 用户行为要求3 创建微服务behavior3.1 微服务创建3.2 添加启动类3.3 创建bootstrap.yml3.4 在nacos中配置redis3.5 引入redis依赖3.6 更新minio 4 跳过 1 long类型精度丢失…

视频批量高效剪辑,支持将视频文件转换为音频文件,轻松掌握视频格式

在数字化时代,视频内容日益丰富,管理和编辑这些视频变得愈发重要。然而,传统的视频剪辑软件往往操作复杂,难以满足高效批量处理的需求。现在,一款全新的视频批量剪辑神器应运而生,它支持将视频文件一键转换…

【vue】slot 匿名插槽 / 具名插槽

slot父组件向子组件传递数据 匿名插槽–直接写 具名插槽–指定名称 父组件中 子组件中&#xff1a; 代码 App.vue <template><h2>App.vue</h2><!-- 匿名插槽 --><Header><a href"1234567890.com">1234567890</a>&…

Bug的定义生命周期

1、bug的定义 你们觉得bug是什么? 软件的Bug狭义概含是指软件程序的漏洞或缺陷&#xff0c; 广义概念除此之外还包括测试工程师或用户所发现和提出的软件可改进的细节(增强性&#xff0c;建议性)、或 与需求文档存在差异的功能实现等。 我们的职责就是&#xff0c;发现这些B…

002nodejs详细安装步骤和npm配置

1、Node.js简介 Node.js 是一个基于 Chrome V8 引擎的 JavaScript 运行时。Node.js 使用高效、轻量级的事件驱动、非阻塞 I/O 模型。它的包生态系统&#xff0c;npm&#xff0c;是目前世界上最大的开源库生态系统。 2、下载Node.js 官方地址&#xff1a;https://nodejs.org/…

vue3 知识点的补充 之 第一节

01 vue2与vue3的区别 vue2 采用object.defuneProperty()实现 对数组不友好 重写了数组的方法&#xff0c;同时无法监听数组length长度的改变。对于对象只能劫持设置好的数据 新增需要使用vue.set vue3 采用proxy进行代理&#xff0c;不需要重写数组的方法 同时可以监听数组长度…

plsql developer 一键格式化sql/美化sql

PL/SQL 格式化工具 以 Oracle SQL Developer 为例&#xff0c;使用一键格式化的步骤如下&#xff1a; 打开 Oracle SQL Developer。在“文件”菜单中&#xff0c;选择“打开文件”&#xff0c;然后选择你的 PL/SQL 文件。打开文件后&#xff0c;你可以通过右键菜单选择“格式…

机器学习——自动驾驶

本章我们主要学习以下内容: 阅读自动驾驶论文采集数据根据论文搭建自动驾驶神经网络训练模型在仿真环境中进行自动驾驶 论文介绍 本文参考自2016年英伟达发表的论文《End to End Learning for Self-Driving Cars》 📎end2end.pdf

全栈的自我修养 ———— 如何发布一个npm包?

创建本地仓库 npm init在此期间会让你添加一些版本信息和名称 登陆npm npm login ——> yinhaodada arx.040208发布 npm publish查询

微服务(基础篇-008-Elasticsearch分布式搜索【上】)

目录 初识elasticsearch&#xff08;1&#xff09; 了解ES&#xff08;1.1&#xff09; 倒排索引&#xff08;1.2&#xff09; es的一些概念&#xff08;1.3&#xff09; 安装es、kibana&#xff08;1.4&#xff09; ik分词器&#xff08;1.5&#xff09; ik分词器的拓展…

RT-Thread内核简介

1、RT-Thread 内核介绍 RT-Thread 内核架构图,内核处于硬件层之上,内 核部分包括内核库、实时内核实现 内核库是为了保证内核能够独立运行的一套小型的类似 C 库的函数实现子集。这部分根据编译器的不 同自带 C 库的情况也会有些不同,当使用 GNU GCC 编译器时,会携带…

在 Elasticsearch 中扩展 ML 推理管道:如何避免问题并解决瓶颈

作者&#xff1a;来自 Elastic Iulia Feroli 是时候考虑语义搜索运营了吗&#xff1f; 无论你是一位经验丰富的搜索工程师&#xff0c;希望探索新的人工智能功能&#xff0c;还是一位机器学习专家&#xff0c;希望更多地利用搜索基础设施来增强语义相似性模型 —— 充分利用这…

jenkins下载安装(mac)

下载官网 具体 直接命令安装 Sample commands: Install the latest LTS version: brew install jenkins-ltsStart the Jenkins service: brew services start jenkins-ltsRestart the Jenkins service: brew services restart jenkins-ltsUpdate the Jenkins version: brew u…

Linux02(项目部署,手动和自动部署,JDK版本问题,安装软件,安装软件,安装JDK,Tomcat,MySQL,Irzsz)

目录 一、安装软件 1. 安装准备工作 1 Linux里的软件安装方式 2 上传软件到Linux 3 拍照虚拟机快照 2. 安装JDK 1 卸载自带jdk 2 解压JDK 3 配置环境变量 4 测试JDK 3. 安装Tomcat 1 解压Tomcat 2 修改防火墙设置 3 测试Tomcat 启动Tomcat 访问Tomcat 查看Tom…

【Jenkins PipeLine】Jenkins PipeLine 联动参数示例

目录 1. Pipeline script&#xff1a; 1.1.代码说明&#xff1a; 2. 实现效果&#xff1a; 3.联动说明&#xff1a; 4.Jenkins安装插件 1. Pipeline script&#xff1a; properties([parameters([[$class: "ChoiceParameter", choiceType: "PT_SINGLE_SELE…

redis的主从复制(docker方式快速入门和实战)

目录 一、主从复制简介 二、配置主从服务器 2.1使用配置文件的形式来主从复制 2.2使用纯代码的方式来进行主从复制&#xff1b; 2.3脱离主服务器 三、一些注意事项 一、主从复制简介 主从复制&#xff0c;是指将一台Redis服务器的数据&#xff0c;复制到其他的Redis服务器…

【opencv】示例-pca.cpp PCA图像重建演示

// 加载必要的头文件 #include <iostream> // 用于标准输入输出流 #include <fstream> // 用于文件的输入输出 #include <sstream> // 用于字符串的输入输出流操作#include <opencv2/core.hpp> // OpenCV核心功能的头文件 #include "o…

Upload-labs(Pass-17--Pass-21)

Pass-17 二次渲染图片马/条件竞争 二次渲染就是在我们上传的图片后&#xff0c;网站会对图片进行二次处理&#xff0c;比如对图片的尺寸、格式、以及网站对图片进行定义的一些要求等进行处理&#xff0c;并且服务器会对里面的内容进行二次替换更新&#xff0c;在处理完后&…