美文网首页
【rust学习】常见的编程概念

【rust学习】常见的编程概念

作者: gearicy | 来源:发表于2022-08-08 23:41 被阅读0次

一、变量和可变性

变量默认是不可改变的(immutable)。这是 Rust 提供给你的众多优势之一,让你得以充分利用 Rust 提供的安全性和简单并发性来编写代码。不过,你仍然可以使用可变变量。
文件名:src/main.rs

fn main() {
   let x=5;
   println!(" x 的值是: {}",x);
   x=6;
   println!(" x 的值是: {}",x);
}

保存并使用cargo run运行,会看到报错信息

 ~/myrust/guessing_game   master  cargo run
   Compiling guessing_game v0.1.0 (/Users/shuai/myrust/guessing_game)
error[E0384]: cannot assign twice to immutable variable `x`
 --> src/main.rs:6:4
  |
4 |    let x=5;
  |        -
  |        |
  |        first assignment to `x`
  |        help: consider making this binding mutable: `mut x`
5 |    println!(" x 的值是: {}",x);
6 |    x=6;
  |    ^^^ cannot assign twice to immutable variable

For more information about this error, try `rustc --explain E0384`.
error: could not compile `guessing_game` due to previous error

尽管变量默认是不可变的,你仍然可以在变量名前添加 mut 来使其可变

fn main() {
   let mut x=5;
   println!(" x 的值是: {}",x);
   x=6;
   println!(" x 的值是: {}",x);
}

二、隐藏

我们可以定义一个与之前变量同名的新变量。Rustacean 们称之为第一个变量被第二个 隐藏(Shadowing) 了,这意味着当您使用变量的名称时,编译器将看到第二个变量。

fn main() {
    let x = 5;

    let x = x + 1;

    {
        let x = x * 2;
        println!("The value of x in the inner scope is: {x}");
    }

    println!("The value of x is: {x}");
}

运行结果

 ✘  ~/myrust/guessing_game   master  cargo run
   Compiling guessing_game v0.1.0 (/Users/shuai/myrust/guessing_game)
    Finished dev [unoptimized + debuginfo] target(s) in 0.34s
     Running `target/debug/guessing_game`
The value of x in the inner scope is: 12
The value of x is: 6

mut 与隐藏的另一个区别是,当再次使用 let 时,实际上创建了一个新变量,我们可以改变值的类型,并且复用这个名字。

fn main() {
    let spaces = "   ";
    let spaces = spaces.len();
}

然而,如果尝试使用 mut,将会得到一个编译时错误,如下所示:

fn main() {
    let mut spaces = "   ";
    spaces = spaces.len();
}

上述代码会报如下错误:

 ~/myrust/guessing_game   master  cargo run
   Compiling guessing_game v0.1.0 (/Users/shuai/myrust/guessing_game)
error[E0308]: mismatched types
 --> src/main.rs:5:14
  |
4 |     let mut spaces = "   ";
  |                      ----- expected due to this value
5 |     spaces = spaces.len();
  |              ^^^^^^^^^^^^ expected `&str`, found `usize`

For more information about this error, try `rustc --explain E0308`.
error: could not compile `guessing_game` due to previous error

相关文章

  • 【rust学习】常见的编程概念

    一、变量和可变性 变量默认是不可改变的(immutable)。这是 Rust 提供给你的众多优势之一,让你得以充分...

  • Rust 学习资源

    Rust编程语言 Rust编程语言(https://doc.rust-lang.org/book/[https:/...

  • Rust语言编程实例100题-066

    Rust语言编程实例100题-066 题目:Rust指针练习。先来理解下引用和借用的概念。引用是作为参数传递给函数...

  • 001 Rust 异步编程,Why Async

    前言 我们之前已经学习过Rust编程基础相关的一些知识,下面进入到Rust异步编程的学习,本节主要介绍为什么需要R...

  • Rust入坑指南:齐头并进(下)

    前文中我们聊了Rust如何管理线程以及如何利用Rust中的锁进行编程。今天我们继续学习并发编程, 原子类型 许多编...

  • RUST 学习日记 第4课 ——Rust规范

    RUST 学习日记 第4课 ——Rust规范 0x00 回顾 上一节咱们了解了Rust的常见IDE,选择一款好的...

  • Rust入坑指南:海纳百川

    今天来聊Rust中两个重要的概念:泛型和trait。很多编程语言都支持泛型,Rust也不例外,相信大家对泛型也都比...

  • 003Rust异步编程,Future trait介绍

    Future介绍 Future是Rust异步编程的核心,Rust异步编程基本都是围绕Future来展开。那么,什么...

  • Rxjava总结

    概念 函数式编程就是一种编程范式,常见的编程范式有命令式编程 函数式编程 和逻辑式编程。。。常见的面向对象编程是一...

  • Rust 学习笔记 - 函数

    Rust 是一门多范式的编程语言,但 Rust 的编程风格是更偏向于函数式的,函数在 Rust 中是“一等公民”。...

网友评论

      本文标题:【rust学习】常见的编程概念

      本文链接:https://www.haomeiwen.com/subject/qhymwrtx.html