简体   繁体   中英

error[E0308]: mismatched types — expected `&str`, found struct `std::string::String`

I am making a tic-tac-toe game. The code is not complete, but I am stuck here. I want to print array_display to the console, but when I assign the string, an error pops-up.

use std::io;

fn main() {
    let mut player1: String = String::new();
    let mut player2: String = String::new();
    let mut positions = ["1", "2", "3", "4", "5", "6", "7", "8", "9"];

    let mut lets_play = true;

    println!("Welcome to tic tac toe");
    println!("Player 1 please select what symbol you want to be : (x or o)");
    io::stdin().read_line(&mut player1);

    player1 = player1.to_lowercase();
    println!("{:?}", player1);

    if player1.trim() == "x" {
        player1 = String::from("x");
        player2 = String::from("o");
        println!("Player 1 is x");
    } else if player1.trim() == "o" {
        player1 = String::from("o");
        player2 = String::from("x");
        println!("Player 1 is o");
    } else {
        println!("Input is not valid");
        lets_play = false;
    }
    if lets_play {
        println!("Let's start the game :");

        print_board(&mut positions);
    } else {
        println!("Please reset the game");
    }
}

fn print_board(arr: &mut [&str]) {
    let mut counter = 0;
    let mut array_display = ["1", "2", "3"];
    let mut array_position = 0;
    let mut string_for_array = String::new();

    for i in 0..arr.len() {
        string_for_array.push_str(arr[i]);
        counter += 1;
        if counter == 3 {
            println!(
                "array_display[{}] value =  {}",
                array_position, string_for_array
            );
            array_display[array_position] = string_for_array.to_string();
            println!("String to push {:?}", string_for_array);
            string_for_array = String::from("");
            println!("Array position {}", array_position);
            array_position += 1;
            counter = 0;
        }
    }
}

Error:

error[E0308]: mismatched types
  --> src/main.rs:52:45
   |
52 |             array_display[array_position] = string_for_array.to_string();
   |                                             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |                                             |
   |                                             expected `&str`, found struct `std::string::String`
   |                                             help: consider borrowing here: `&string_for_array.to_string()`

The current issue in your code is that string_for_array.to_string() creates a new String , but the array_display array contains &str references.

The suggestion the compiler gives here (replacing with &string_for_array.to_string() ) does not work, because the result of .to_string() will be freed at the end of the line and you would have an invalid &str reference.

So, the issue is: some variable needs to own that string. Since string_for_array is modified later, it can't be used. The natural choice is array_display (because that's where that string will live anyway). So, first modify this array to contain owned String s instead of &str references:

    let mut array_display = ["1".to_owned(), "2".to_owned(), "3".to_owned()];

And then the rest of the code will compile.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM