【问题标题】:error[E0308]: mismatched types — expected `&str`, found struct `std::string::String`错误 [E0308]:不匹配的类型 — 预期的 `&str`,找到了结构 `std::string::String`
【发布时间】:2020-03-11 02:39:24
【问题描述】:

我正在制作井字游戏。代码不完整,但我被困在这里。我想在控制台打印array_display,但是当我分配字符串时,会弹出一个错误。

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[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()`

【问题讨论】:

  • 帮助:考虑在这里借用:&string_for_array.to_string() — 当你尝试这个时发生了什么?
  • 如果你执行它可能会更好看,但我得到错误[E0716]:temporary value dropped while borrowed ,array_display[array_position] = borrow later used here ,&string_for_array.to_string(); = 创建一个临时的,在使用时释放

标签: rust type-mismatch


【解决方案1】:

您代码中的当前问题是string_for_array.to_string() 创建了一个新的String,但array_display 数组包含&str 引用。

编译器在此处给出的建议(替换为&string_for_array.to_string())不起作用,因为.to_string() 的结果将在行尾被释放,您将获得一个无效的&str 引用。

所以,问题是:某些变量需要拥有该字符串。由于string_for_array是后期修改的,所以不能使用。很自然的选择是array_display(因为那是那个字符串无论如何都会存在的地方)。所以,首先修改这个数组以包含拥有的Strings 而不是&str 引用:

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

然后剩下的代码就编译好了。

【讨论】:

  • 这行得通,仍然是生锈的新手,仍然很难理解借用、参考的东西,我不知道 to_owned() ,我也见过 .clone() 等。我会进一步调查,谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-04
  • 1970-01-01
  • 2016-11-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多