【发布时间】: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