Hello Rust Community!
As an exercise in learning Rust I am trying to make a simple console program. I should be able to acquire data, store and modify it by user command.
I have run into troubles with the second part. Somehow, when trying to store user input, I am running into "borrowed value does not live long enough" error.
Below is a program snippet that reproduces the problem:
use std::io;
use std::collections::HashMap;
fn main() {
// Structure to store user input in during loop
let mut map: HashMap<&str, &str> = HashMap::new();
loop {
// User input should be a command of several words,
// like "add Alice to Wonderland"
let mut input = String::new();
io::stdin().read_line(&mut input)
.expect("Error reading input");
// The input string is separated to individual words vector
// like "add" "Alice" "to" "Wonderland"
// to simplify further processing
let mut input: Vec<&str> = input.split_whitespace().collect();
println!("{:?}", input);
// No input ends cycle and finishes program
if input.is_empty() { break }
// Here:
// Trying to store value in HashMap causes lifetime error
map.entry(input[1]).or_insert(input[3]);
}
}
And the subsequent compilation error:
error[E0597]: `input` does not live long enough
--> src/main.rs:18:36
|
18 | let mut input: Vec<&str> = input.split_whitespace().collect();
| ^^^^^ borrowed value does not live long enough
...
24 | map.entry("some_key").or_insert(input[0]);
| --- borrow used here, in later iteration of loop
25 | }
| - `input` dropped here while still borrowedThis text will be hidden
I have been looking for solution, but couldn't find the case of processing user input in loop, while storing it in a permanent structure.
P.S. Also, how do I highlight syntax here?