当前位置: 首页 > news >正文

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.lryc.cn/news/107854.html

相关文章:

  • 向“数”而“深”,联想凌拓的“破局求变”底气何来?
  • pytorch实战-图像分类(二)(模型训练及验证)(基于迁移学习(理解+代码))
  • b 树和 b+树的理解
  • 正则表达式 —— Awk
  • 国芯新作 | 四核Cortex-A53@1.4GHz,仅168元起?含税?哇!!!
  • 【MyBatis】 框架原理
  • 三、线性工作流
  • 2023华数杯数学建模A题思路 - 隔热材料的结构优化控制研究
  • Zabbix分布式监控Web监控
  • PHP从入门到精通—PHP开发入门-PHP概述、PHP开发环境搭建、PHP开发环境搭建、第一个PHP程序、PHP开发流程
  • 【LeetCode-中等】722. 删除注释
  • rust里如何判断字符串是否相等呢?
  • python基本知识学习
  • vue3和typescript_组件
  • Qt+联想电脑管家
  • 论文阅读-BotPercent: Estimating Twitter Bot Populations from Groups to Crowds
  • 用于永磁同步电机驱动器的自适应SDRE非线性无传感器速度控制(MatlabSimulink实现)
  • Spring Cloud+Spring Boot+Mybatis+uniapp+前后端分离实现知识付费平台免费搭建 qt
  • 删除注释(力扣)
  • 阿里云AK创建
  • OC与Swift的相互调用
  • 某银行软件测试笔试题
  • SpringMVC概述、SpringMVC的工作流程、创建SpringMVC的项目
  • 一文说清楚支付架构
  • 【Golang 接口自动化00】为什么要用Golang做自动化?
  • Android 架构模式如何选择
  • 深入了解 LoRaWAN® B 类设备
  • KK集团再闯港交所:引领潮流零售市场,2023年一季度业绩增势显著
  • Vue中的组件渲染
  • docker 保存和载入镜像