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

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

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

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.mfbz.cn/a/554445.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

HackMyVM-suidy

目录 信息收集 arp nmap WEB web信息收集 gobuster 目录批量查看 hydra ssh连接 提权 系统信息收集 提权 信息收集 arp ┌─[rootparrot]─[~/HackMyVM] └──╼ #arp-scan -l Interface: enp0s3, type: EN10MB, MAC: 08:00:27:16:3d:f8, IPv4: 192.168.9.115 St…

【网络通信基础】网络中的常见基本概念

目录 一、IP地址 二、端口号 三、协议 四、五元组 五、协议分层 1. OSI 模型 2. TCP/CP五层(或四层)模型 3. 网络设备所在分层 六、封装和分用 封装(Encapsulation) 分用(Multiplexing) 一、IP…

多目标环形粒子群算法和多目标遗传算法跑MOCEC2020(24个多目标测试函数,matlab代码,多个评价指标)

本号从现在起可以定制使用评估次数改进单目标群体算法,需要的私,价格贵,质量高。 目录: 一、多目标环形粒子群算法MO_Ring_PSO_SCD 二、多目标遗传算法NSGAII 三、MOCEC2020的24个多目标测试函数 四、实验结果 五、代码获取…

C语言进阶课程学习记录-函数指针的阅读

C语言进阶课程学习记录-函数指针的阅读 5个标识符含义解析技巧 本文学习自狄泰软件学院 唐佐林老师的 C语言进阶课程,图片全部来源于课程PPT,仅用于个人学习记录 5个标识符含义解析 int (*p1) (int* , int (*f) ( int* ) );定义了指针p1,指向函数&#…

javaWeb智能医疗管理系统

简介 在当今快节奏的生活中,智能医疗系统的崛起为医疗行业带来了一场革命性的变革。基于JavaWeb技术开发的智能医疗管理系统,不仅为医疗机构提供了高效、精准的管理工具,也为患者提供了更便捷、更个性化的医疗服务。本文将介绍一个基于SSM&a…

树莓集团产业生态建设之特色产业服务:人才项目转化中心

树莓集团在产业生态建设中,积极输出特色产业服务——人才项目转化中心。该中心依托数字产业园致力于推动创新创业工作,通过链接产业人才聚集地与树莓认证的导师库体系,为人才及相关课题项目提供全方位的服务。 树莓集团人才项目转化中心以人…

Python学习(四)文件操作

文件操作 想想我们平常对文件的基本操作,大概可以分为三个步骤(简称文件操作三步走): ① 打开文件 ② 读写文件 ③ 关闭文件 注意:可以只打开和关闭文件,不进行任何读写 在Python,使用open函数,可以打开一个已经存在的文件&…

IP爬虫代理服务器是什么以及为什么使用爬虫代理?

在网络抓取领域,爬虫代理发挥着关键作用。 但它们到底是什么? 从本质上讲,爬虫代理是位于网络抓取工具和目标网站之间的中间服务器。 该中间服务器充当盾牌,提供匿名性,并允许您访问网站并提取数据,而无需透…

IDEA配置Maven环境

黑马程序员JavaWeb开发教程 文章目录 如果当前有已经打开项目的话,File -> Close Project 到以下页面之后选择 Customize -> All settings… 配置maven的安装目录,maven的配置文件,maven的本地仓库(修改完成之后一定要先…

mybatis进阶篇-执行CRUD操作-typeAliases别名-接口绑定

目录结构 所需jar包 https://download.csdn.net/download/weixin_44201223/89160447?spm1003.2166.3001.6637.1 1.创建数据表(book) # 创建book表 create table book(id int auto_increment primary key,name varchar(255) ,price double ,num int )…

Linux OpenSSH最新版9.7p1升级操作详细教程

原创声明:非本人许可,谢绝转载! 1.背景说明 前几天与朋友闲聊中得知他朋友圈有服务器因OpenSSH漏洞遭受攻击的事情,OpenSSH重要性这里就不废话了,在网上一查,公布的漏洞还真不少,其中还有不少…

RK3588平台开发系列讲解(PCIe开发篇2)

根据原理图填写DTS 原理图是基于IO信号的视⻆来描述硬件,IO信号是跟PHY的index强相关的,前⾯提到RK3588的controller和PHY的index可能不⼀致,所以看原理图的时候需要特别注意这⼀点。这⾥给出⼀些填写建议,并通过⽰例说明如何将原…

socket通信基础讲解及示例-C

socket通信之C篇 服务端与客户端简介 socket通信服务端与客户端通信模型通信实战server(服务端)创建client(客户端)创建 函数详解创建套接字 socket绑定端口bind进入监听状态listen获取客户端连接请求accept接收网络数据read发送数…

音素与视素(Viseme)

什么是视素(视位) 音素(Phoneme),是人类语言中能够区别意义的最小声音单位。视素(Viseme),是指与某一音素相对应的嘴、舌头、下腭等可视发音器官所处的状态。Viseme是MPEG-4 标准提出来的概念。 有时Viseme也翻译为视位。下面会…

JavaEE初阶Day 10:多线程(8)

目录 Day 10:多线程(8)单例模式阻塞队列1. 生产者消费者模型1.1 生产者消费者模型解耦合1.2 生产者消费者模型削峰填谷 2. 生产者消费者代码3. 阻塞队列实现 Day 10:多线程(8) 单例模式 单例模式&#xf…

springboot中mongodb连接池配置-源码分析

yml下spring.data.mongodb 以前mysql等在spring.xxx下配置,现在springboot新版本(小编3.2.3)在spring.data.xxx下了,如下所示,mongodb的配置在spring.data.mongodb下: 连接池相关参数配置-源码分析 拼接在…

vue3 删除对象中的属性,可以使用js里的delete,但需注意ts定义对象类型!

如上如,当使用delete 删除stateData中的属性时, 报错,意思为 TypeScript 错误“‘delete’ 运算符的操作数必须是可选的 什么原因呢?是因为我偷懒 缺少了ts定义类型 方法一: (不推荐) delete …

【网络安全 | 信息收集】JS文件信息收集工具LinkFinder安装使用教程

文章目录 前言安装教程使用教程 前言 JavaScript文件可能会泄露敏感信息,如注释中的机密信息、内部IP地址,以及包含未授权访问或其他漏洞的URL。手动检查这些信息效率低下,而该工具——LinkFinder,可用于自动收集JavaScript文件中…

Windows10系统中忘记MySQL数据库root权限登录密码

本文档所使用的MySQL版本为MySQL5.7>> mysqld_safe --skip-grant-tables&mysql -u root mysql在命令行中使用上面的命令登录MySQL,其中--skip-grant-tables允许用户跳过权限表进行无密码登录 >> UPDATE user SET authentication_stringPASSWORD(&q…

2024 年 Web 前端开发趋势

希腊哲学家赫拉克利特认为,变化是生命中唯一不变的东西。这句话适用于我们的个人生活、行业和职业领域。 尤其是前端开发领域,新技术、开发趋势、库和框架不断涌现,变化并不陌生。最近发生的一些事件正在改变开发人员构建网站和 Web 应用的方…
最新文章