I wanted to input a value and assign it to a variable that is immutable (should be). How can I do that?
Currently, I am doing this:
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);
}
But without &mut
, it generates an error: types differ in mutability
. If I remove mut
from the declaration I get an error like this:
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`
How can I have something like this:
let input_var = input(); // or some other function that inputs the value and directly assigns it.
I tried reading the official documentation, the first few chapters, but in vain.