Rust- 变量绑定

In Rust, you bind values to a variable name using the let keyword. This is often referred to as “variable binding” because it’s like binding a name to a value.

Here’s a simple example:

let x = 5;

In this example, x is bound to the value 5. By default, bindings are immutable in Rust. If you try to reassign x to a different value, you’ll get a compile-time error. If you want a binding to be mutable, you can use the mut keyword:

let mut x = 5;
x = 10; // This is okay because x is mutable

In Rust, you can also bind a variable to an expression. The expression will be evaluated, and the resulting value will be bound to the variable:

let x = 5 * 5; // x is bound to the value 25

Variable binding in Rust also allows for pattern matching, which enables more complex types of binding. For example, if you have a tuple, you can bind the individual elements of the tuple to different variables:

let (x, y) = (1, 2); // x is bound to 1, and y is bound to 2

Rust also requires that all variables be initialized before they are used, which prevents undefined behavior.

Lastly, Rust features a system of “shadowing” where a new variable can be declared with the name of a previous variable, effectively creating a new variable that “shadows” the old one.

let x = 5;
let x = x + 5; // x is now 10
let x = x * 2; // x is now 20

Each x is a new variable that shadows the previous x. This is not the same as mutation because these xs are new variables, they just happen to have the same name as the previous variable.

fn main() {/*变量是有作用域的,也就是在一个代码块中生存。代码块 {}, 也允许变量遮蔽。*/// main 函数中let spend = 1;{// 只存在本代码块中let target = "面向对象";println!("内部 {}", target);    // 内部 面向对象// 遮蔽了外面的spendlet spend = 2.0;println!("内部 {}", spend);     // 内部 2}// target在此作用域是不存在的// println!("外部 {}", target);println!("外部 {}", spend);         // 外部 1// 遮蔽了spendlet spend = String::from("学习时间1小时");println!("外部 {}", spend);         // 外部 学习时间1小时let spend2;{let x = 2;spend2 = x * x;}println!("spend2: {}", spend2);     // spend2: 4let spend3;// println!("spend3: {}", spend3); // 报错,使用了未初始化的绑定spend3 = 1;println!("another binding spend3: {}", spend3); // another binding spend3: 1// 冻结 资源存在使用的引用时,在当前作用域中这一资源是不可被修改的。let mut spend4 = Box::new(1);let spend5 = &spend4;   // `spend4` is borrowed herespend4 = Box::new(100); // `spend4` is assigned to here but it was already borrowedprintln!("{}", spend4);println!("{}", spend5);
}

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

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

相关文章

类变量和类方法

类变量和类方法 引入 思考:有一群小孩在玩堆雪人,不时有新的小孩加入,请问如何知道现在有多少小孩在玩? 静态变量内存分析 1、静态变量被对象共享 2、静态变量可能在堆中,也可能在方法区的静态域中,这…

在 Windows 上搭建 NTP 服务器

文章目录 一、基础环境二、适用场景三、操作步骤四、常用的NTP服务器五、参考资料 版权声明:本文为博主原创文章,于2023年7月30日首发于CSDN,转载请附上原文出处链接和本声明。本文链接:https://blog.csdn.net/u011046671 一、基础…

Flutter 的线程模型和异步原理

本文字数::36130字 预计阅读时间:91分钟 在Android应用中, 用户时常会遇到界面卡顿的情况,非常影响用户的体验。作为Android开发肯定都知道:应用在主线程里做了大量的耗时操作(例如文件读写, 数…

stable diffusion打造自己专属的LORA模型

通过Lora小模型可以控制很多特定场景的内容生成。 但是那些模型是别人训练好的,你肯定很好奇,我也想训练一个自己的专属模型(也叫炼丹~_~)。 甚至可以训练一个专属家庭版的模型(family model&…

RT1052 的周期定时器

文章目录 1 PIT 周期中断定时器2 PIT定时器的使用3 PIT定时器配置3.1 PIT 时钟使能。3.1.1 CLOCK_EnableClock 3.2 初始化 PIT 定时器3.2.1 PIT_Init 3.3 设置 通道 0 的 加载值3.3.1 PIT_SetTimerPeriod 3.4 使能 通道 0 的中断3.4.1 PIT_EnableInterrupts 3.5 开启 PIT 定时器…

PysparkNote006---pycharm加载spark环境

pycharm配置pyspark环境,本地执行pyspark代码 spark安装、添加环境变量不提了 File-Settings-Project-Project Structure-add content root添加如下两个路径 D:\code\spark\python\lib\py4j-0.10.7-src.zipD:\code\spark\python\lib\pyspark.zip 2023-07-26 阴 于…

Redis缓存预热

说明:项目中使用到Redis,正常情况,我们会在用户首次查询数据的同时把该数据按照一定命名规则,存储到Redis中,称为冷启动(如下图),这种方式在一些情况下可能会给数据库带来较大的压力…

监听器Listener详解

1、Listener 是由Java编写的WEB组件,主要完成对内置对象状态的变化 (创建、销毁)和属性的变化 进行监听,做进一步的处理作用:主要对session和application内置对象监听。 2、对application监听 package cn.mldn.lxh.l…

AcWing 算法基础课二 数据结构 链表 栈 队列 并查集 哈希表

单链表. AcWing. 826.单链表 import java.util.Scanner; public class Main{static int[] e new int[100010];//结点i的值static int[] ne new int[100010];//结点i的next指针static int idx,head;//head是头结点,idx存当前已经用到了哪个点public static void i…

【简化程序设计】C++STL“容器适配器“之栈和队列

【STL】容器适配器之栈和队列 stack的介绍和使用stack的介绍stack的使用stack的模拟实现 queue的介绍和使用queue的介绍queue的使用queue的模拟实现 priority_queue的介绍和使用priority_queue的介绍priority_queue的使用priority_queue的模拟实现 容器适配器什么是容器适配器&…

Vision Transformer (ViT):图像分块、图像块嵌入、类别标记、QKV矩阵与自注意力机制的解析

作者:CSDN @ _养乐多_ 本文将介绍Vision Transformers (ViT)中的关键点。包括图像分块(Image Patching)、图像块嵌入(Patch Embedding)、类别标记、(class_token)、QKV矩阵计算过程、余弦相似度(cosine similarity)、Softmax、自注意力机制等概念。主要介绍QKV矩阵…

C# XML 的读写以及和JSON对比

通过我们进行跨平台传输,我们需要把某一个平台特有的数据类型转化为一种通用的数据类型序列化和反序列化 通用形式有两种: 《1》JSON:是一种以键值形式组成 《2》XML:可扩展标记语言 XML文件格式要求: 《1》头部需要有…

基于x-scan扫描线的3D模型渲染算法

基于x-scan算法实现的z-buffer染色。c#语言&#xff0c;.net core framework 3.1运行。 模型是读取3D Max的obj模型。 x-scan算法实现&#xff1a; public List<Vertex3> xscan() {List<Vertex3> results new List<Vertex3>();SurfaceFormula formula g…

NFS、FTP、SMB、WebDav、DLNA协议区别

文章目录 NFSFTP/SFTP/SCPSMB/SambaWebDAVDLNA总结 随着智能化互联时代的来临&#xff0c;家中的智能设备越来越多&#xff1a;电视机、平板、游戏主机、电脑、手机等遍及家中各个角落&#xff0c;同时设备之间共享数据的需求变的越来越强烈。比如同步、备份手机上的照片和视频…

代码随想录Day53动态规划part14|1143.最长公共子序列|1035.不相交的线|53. 最大子序和 动态规划

1143.最长公共子序列 也不考虑顺序&#xff0c;元素之间可以不连续和718很相似&#xff0c;只不过这题要累加不连续的情况 1035.不相交的线 套上一题的程序可以通过&#xff0c;但是实际没有特别理解 53. 最大子序和 动态规划 之前用贪心做的&#xff0c;一旦sum<0&…

springboot log4j2日志 配置路径

一、log4j2 日志由xml配置&#xff0c;如果想改日志路径&#xff0c; 没办法和application.prop 文件读取参数 处理解决办法 二、1、默认解决办法 xml配置死路径&#xff0c;且测试与生产保持一致 <?xml version"1.0" encoding"UTF-8"?> <!…

从使用回溯分割字符串的技巧到前向搜索

题目 131. 分割回文串 给你一个字符串 s&#xff0c;请你将 s 分割成一些子串&#xff0c;使每个子串都是 回文串 。返回 s 所有可能的分割方案。 回文串 是正着读和反着读都一样的字符串。 答案&#xff1a; class Solution {boolean[][] f;List<List<String>>…

【多线程中的线程安全问题】线程互斥

1 &#x1f351;线程间的互斥相关背景概念&#x1f351; 先来看看一些基本概念&#xff1a; 1️⃣临界资源&#xff1a;多线程执行流共享的资源就叫做临界资源。2️⃣临界区&#xff1a;每个线程内部&#xff0c;访问临界资源的代码&#xff0c;就叫做临界区。3️⃣互斥&…

js 下载url返回的excel数据,并解析为json

XLSX GitHub地址&#xff1a;https://github.com/SheetJS/sheetjs/blob/github/dist/xlsx.full.min.js 需要先引入&#xff1a;XLSX.full.min.js // 下载文件的请求 fetch(downloadFileUrl).then(response > {return rsp.blob() }).then(data > {let reader new FileR…

【密码学】三、AES

AES 1、AES产生2、数学基础2.1有限域GF(2^8^)2.1.1加法运算2.1.2乘法运算2.1.3x乘运算2.1.4系数在GF(2^8^)上的多项式 3、AES算法描述3.1字节代换3.2行移位3.3列混合3.4轮密钥加3.5密钥扩展 1、AES产生 征集AES算法的活动&#xff0c;目的是确定一个非保密的、公开的、全球免费…