【问题标题】:Are all inputs supposed to be mutable in Rust?Rust 中的所有输入都应该是可变的吗?
【发布时间】:2020-02-15 15:29:44
【问题描述】:

我想输入一个值并将其分配给一个不可变的变量(应该是)。我该怎么做?

目前,我正在这样做:

use std::io;

fn main() {
    let mut a = 0;
    let mut b = 1;
    let mut nth_term = String::new();
    io::stdin().read_line(&mut nth_term);
}

但是没有&mut,它会产生一个错误:types differ in mutability。如果我从声明中删除 mut,我会收到如下错误:

error[E0308]: mismatched types
 --> src/main.rs:5:27
  |
5 |     io::stdin().read_line(&nth_term).expect("I/O error");
  |                           ^^^^^^^^^ types differ in mutability
  |
  = note: expected mutable reference `&mut std::string::String`
                     found reference `&std::string::String`

我怎么会有这样的东西:

let input_var = input(); // or some other function that inputs the value and directly assigns it.

我尝试了reading the official documentation, the first few chapters,,但没有成功。

【问题讨论】:

    标签: rust


    【解决方案1】:

    Rust 中的可变性遵循名称,而不是值。因此,如果您有一个绑定到可变变量的值,并且您希望它是不可变的,那么您所要做的就是重新绑定它:

    fn main() {
        let mut nth_term = String::new();
        io::stdin().read_line(&mut nth_term).expect("I/O error");
        let nth_term = nth_term;
        //  ^^^^^^^^-- no `mut`
    }
    

    将一个值重新绑定到具有不同可变性的同名是很常见的(请参阅What does 'let x = x' do in Rust?)。

    您还可以将原始绑定放在块表达式中以最小化mut 变量的范围:

    fn main() {
        let nth_term = {
            let mut nth_term = String::new();
            io::stdin().read_line(&mut nth_term).expect("I/O error");
            nth_term
        };
    }
    

    BufRead::read_line 以这种方式定义,因此您无需为读取的每个新行分配一个新的String。该方法必须采用&mut,因为它可以增长字符串。尽管您可以使用 stdin().lines() 迭代输入行,这确实产生了拥有的 Strings,但没有标准 I/O 函数可以从标准输入读取单行并返回 String(您可以简单地绑定到非mut 变量)。当然,如果您发现自己经常这样做,您可以编写自己的函数,其中包含 mut 并返回,例如,io::Result<String>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-10-15
      • 2010-12-18
      • 1970-01-01
      • 2012-03-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多