Home > front end >  How to add user input from inside a while loop to a vector in Rust?
How to add user input from inside a while loop to a vector in Rust?

Time:12-17

I am making a program where the user enters words that get saved to a vector for future reference. Basically, I have a while loop that as long as the user doesn't desire to end the loop, will allow the user to keep entering words and have them inserted into a vector. However, I keep getting error messages, relating to mutable and immutable or ownership issues.

The following is my code.

use std::io;

fn CarryOn()->i8{ // function where it asks the user if he or she wants to stop input or not
    println!("Do you want to continue ending words to the glossary?");
    println!("Press 1 for yes, enter any other number for no.");
    let mut s1 = String::new();
    io::stdin().read_line(&mut s1).expect("Failed");
    let n1:i8 = s1.trim().parse().expect("Not a valid Number");
    return n1;
}

fn main(){
    let mut words: Vec<String> = Vec::new();
    println!("What's your name?");
    let mut name = String::new();
    io::stdin().read_line(&mut name).expect("Failed");
    println!("Welcome {}",name);
    let mut LastWord = 1;
    let mut WordInput = String::new();
    words.push("Hello".to_string()); // backup, just in case the user doesn't add a word.
    while LastWord == 1 {
        println!("Please type a word to add to the glossary");
        io::stdin().read_line(&mut WordInput).expect("Failed");
        WordInput.make_ascii_lowercase();
        println!("{}",WordInput);
        words.push(WordInput); //line of error; without, program runs fine, but program is useless
        LastWord = CarryOn();
    }
}

Here is the error I got:

error[E0382]: borrow of moved value: WordInput --> src/main.rs:23:31 | 19 | let mut WordInput = String::new(); | ------------- move occurs because WordInput has type String, which does not implement the Copy trait ... 23 | io::stdin().read_line(&mut WordInput).expect("Failed"); | ^^^^^^^^^^^^^^ value borrowed here after move ... 27 | words.push(WordInput); | --------- value moved here, in previous iteration of loop

I have tried a lot of different solutions, like using &str, various usage of mutable and immutables, but nothing fixes the issue. Is there a fix to this issue, or should I try something else?

CodePudding user response:

A "borrow of moved value" error occurs when a variable's ownership is moved into a different scope, even though it is used later on in the same scope.

To explain for this example, what happens is that on a hypothetical first iteration of the while loop:

  1. WordsInput is pushed into words which means that WordsInput can no longer refer to that string. Therefore...
  2. ...On a next iteration, WordsInput does not refer to anything, even though an attempt to access it is made here.

Rust's compiler detects that this occurs in your code and does not allow it to compile.

A way to solve this is to push WordsInput.clone() into words instead of WordsInput. The clone method creates a new instance of a string which is moved into the words scope, while leaving WordsInput in its own scope.

  • Related