Rust- 变量绑定

这篇具有很好参考价值的文章主要介绍了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.文章来源地址https://www.toymoban.com/news/detail-621212.html

fn main() {
    /*
        变量是有作用域的,也就是在一个代码块中生存。
        代码块 {}, 也允许变量遮蔽。
     */

    // main 函数中
    let spend = 1;
    {
        // 只存在本代码块中
        let target = "面向对象";
        println!("内部 {}", target);    // 内部 面向对象

        // 遮蔽了外面的spend
        let spend = 2.0;
        println!("内部 {}", spend);     // 内部 2
    }

    // target在此作用域是不存在的
    // println!("外部 {}", target);
    println!("外部 {}", spend);         // 外部 1

    // 遮蔽了spend
    let spend = String::from("学习时间1小时");
    println!("外部 {}", spend);         // 外部 学习时间1小时

    let spend2;
    {
        let x = 2;
        spend2 = x * x;
    }
    println!("spend2: {}", spend2);     // spend2: 4

    let 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 here
    spend4 = Box::new(100); // `spend4` is assigned to here but it was already borrowed
    println!("{}", spend4);
    println!("{}", spend5);
}

到了这里,关于Rust- 变量绑定的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • Rust教程:How to Rust-变量

    本文为第1篇 本专栏是优质Rust技术专栏,推荐精通一门技术栈的蟹友,不建议完全无计算机基础的同学 感谢Rust圣经开源社区的同学,为后来者提供了非常优秀的Rust学习资源 本文使用: 操作系统macOS Sonoma 14 / Apple M1 编译器:Rustc Cargo 感谢一路相伴的朋友们,感谢你们的支持

    2024年03月15日
    浏览(39)
  • Rust 笔记:Rust 语言中的常量与变量

    Rust 笔记 Rust 语言中的常量与变量 作者 : 李俊才 (jcLee95):https://blog.csdn.net/qq_28550263?spm=1001.2101.3001.5343 邮箱 : 291148484@163.com 本文地址 :https://blog.csdn.net/qq_28550263/article/details/130875912 【介绍】:本文介绍 Rust 语言中的常量与变量。 上一节:《 上一节标题 》 | 下一节:《

    2024年02月06日
    浏览(49)
  • 【Rust 基础篇】Rust可变静态变量:全局状态的可变性管理

    Rust是一种以安全性和高效性著称的系统级编程语言,其设计哲学是在不损失性能的前提下,保障代码的内存安全和线程安全。为了实现这一目标,Rust引入了\\\"所有权系统\\\"、\\\"借用检查器\\\"等特性,有效地避免了常见的内存安全问题。然而,有时候我们需要在程序的整个生命周期

    2024年02月15日
    浏览(27)
  • Rust-变量

    Rust的变量必须先声明后使用。对于局部变量,最常见的声明语法为: 与传统的C/C++语言相比,Rust的变量声明语法不同。这样设计主要有以下几个方面的考虑。 语法分析更容易 从语法分析的角度来说,Rust的变量声明语法比C/C++语言的简单,局部变量声明一定是以let开头

    2024年01月21日
    浏览(45)
  • Rust 学习笔记 - 变量声明与使用

    任何一门编程语言几乎都脱离不了:变量、基本类型、函数、注释、循环、条件判断,这是一门编程语言的语法基础,只有当掌握这些基础语法及概念才能更好的学习 Rust。 Rust 是一种强类型语言,但在声明变量时,不总是需要显式声明类型,这是因为 Rust 的编译器有类型推

    2024年02月19日
    浏览(32)
  • Rust变量、常量声明与基本数据类型

    Rust是一门系统级别的编程语言,注重安全性、性能和并发。在这篇博客中,我们将介绍Rust中的变量、常量声明以及基本数据类型,并通过示例说明每一种类型的用法。 在Rust中,使用 let 声明变量。变量默认是不可变的,要使其可变,需要使用 mut 。 常量使用

    2024年01月18日
    浏览(50)
  • Rust语法:变量,函数,控制流,struct

    可变与不可变变量 Rust中使用let来声明变量,但是let声明的是不可变变量,如果想要可变,则需要加上mut 变量与常量 常量与不可变变量一样都是不能被修改的,但是他与不可变变量有很多区别。 常量用const声明,且必须标注其类型 常量不能加mut修饰 常量可以在任何作

    2024年02月13日
    浏览(31)
  • 【Rust】001-基础语法:变量声明及数据类型

    “一切能用 Rust 重写的项目都将或者正在用 Rust 重写” Rust 入门与实践:https://juejin.cn/book/7269676791348854839?utm_source=course_list 代码演示 执行结果 依赖 Cargo.toxml 代码 执行命令 根目录执行 整型标量类型 只要记得最低从 8 开始,到 128 结束(当然,正常情况下我们最多用到 64,

    2024年02月10日
    浏览(38)
  • Rust - 变量与数据的交互方式(clone)

     在上一篇文章中我们介绍了变量与数据的交互方式-move,通过底层原理我们知道Rust 永远也不会自动创建数据的 “深拷贝”。因此,任何  自动 的复制可以被认为对运行时性能影响较小。 但是如果我们  确实 需要深度复制  String 中堆上的数据,而不仅仅是栈上的数据,可

    2024年01月22日
    浏览(65)
  • rust 初识基础: 变量、数据类型、函数、所有权、枚举

    了解到 rust 和 WebAssembly 的结合使用,可以构建前端应用,而且性能也比较好。初步学习使用 rust 是预编译静态类型语言。 官网下载 rust-CN , 大致了解下为什么选择:高性能、可靠性、生产力。 打开控制台啊,执行安装 (mac 系统,windwos 或其他系统查看官网) 安装成功时,会打

    2024年02月07日
    浏览(48)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包