-3

The idea is to send a set of characters of a vector and let the function display the current correct guesses.

Here is my main:

fn main() {
    let mut guessedLetters = vec![];
    displayWord(guessedLetters);
}

And here is the function:

fn displayWord(correctGuess: Vec<char>) {
    let mut currentWord = String::new();

    for x in 0..5 {
        currentWord.push(correctGuess[x]);
    }
    println!("Current guesses: {}", currentWord);
}

I don't know what I'm supposed to write inside the parameters of displayWord.

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
  • 2
    I am also a bit confused. What is this function supposed to do? Why do you write a function if you don't know how you would call it? – mcarton Aug 21 '18 at 21:29
  • It's supposed to print out the current progress on how many characters you got right, also known as Hanging man. I know how i want to call it, i just don't know what to put inside of the parameters (). Do i type vec? vector? – Charles Jonasson Aug 21 '18 at 21:37
  • What's wrong with what you have? [This compiles now.](http://play.rust-lang.org/?gist=2c0b8b1c75b668066e07087efa89da6c&version=stable&mode=debug&edition=2015) – trent Aug 21 '18 at 22:02
  • That's wierd, here's the full code though that doesn't work: http://play.rust-lang.org/?gist=d17b180243a732f3172f10f1b6e5e1b1&version=stable&mode=debug&edition=2015 – Charles Jonasson Aug 21 '18 at 22:08
  • 1
    You may wish to know about Rust's [naming conventions](https://rust-lang-nursery.github.io/api-guidelines/naming.html). Use `lower_case` for local variables and functions. – E_net4 Aug 22 '18 at 17:37
  • 1
    Moreover, iterating on the collection itself is preferred over using indices: `for v in correct_guess { }`. – E_net4 Aug 22 '18 at 19:09

1 Answers1

2

There's a couple of things wrong with your code.

The first error is pretty straight forward:

--> src/main.rs:38:25
   |
38 |             displayWord(guessed_Letters);
   |                         ^^^^^^^^^^^^^^^ expected char, found enum     `std::option::Option`
   |
   = note: expected type `std::vec::Vec<char>`
              found type `std::vec::Vec<std::option::Option<char>>`

The function you wrote is expecting a vector a characters ... but you're passing it a vector of Option<char>. This is happening here:

guessed_Letters.push(line.chars().nth(0));

According to the documentation, the nth method returns an Option. The quick fix here is to unwrap the Option to get the underlying value:

guessed_Letters.push(line.chars().nth(0).unwrap());

Your next error is:

error[E0382]: use of moved value: `guessed_Letters`
  --> src/main.rs:38:25
   |
38 |             displayWord(guessed_Letters);
   |                         ^^^^^^^^^^^^^^^ value moved here in previous iteration     of loop
   |
   = note: move occurs because `guessed_Letters` has type `std::vec::Vec<char>`, which does not implement the `Copy` trait

This is transferring ownership of the vector on the first iteration of the loop and the compiler is telling you that subsequent iterations would be in violation of Rust's ownership rules.

The solution here is to pass the vector by reference instead:

displayWord(&guessed_Letters);

..and your method should also accept a reference:

fn displayWord(correctGuess: &Vec<char>) {

  let mut currentWord = String::new();

  for x in 0..5 { 
    currentWord.push(correctGuess[x]);
  }
  println!("Current guesses: {}", currentWord);
}

This can be shortened to use a slice and still work:

fn displayWord(correctGuess: &[char]) {
Tim Diekmann
  • 7,755
  • 11
  • 41
  • 69
Simon Whitehead
  • 63,300
  • 9
  • 114
  • 138
  • Thank you alot, i got rid of the errors. There is one issue remaining that i understand even less than the previous ones, shown in the picture: https://gyazo.com/70d83c6ef130fd8988f18d807d5e4a6f Code: https://gist.github.com/rust-play/9c4229aa87274ea338df32b0ea2ad882 I'll mark your comment finished either way, because you helped me with the original problem, again i appreciate it alot, thanks so much! – Charles Jonasson Aug 21 '18 at 23:08
  • 3
    [Why is it discouraged to accept a reference to a String (&String), Vec (&Vec) or Box (&Box) as a function argument?](https://stackoverflow.com/q/40006219/155423) – Shepmaster Aug 21 '18 at 23:35