小编dre*_*eid的帖子

错误[E0597]:借用的值在 While 循环中的生存时间不够长

我对 Rust 很陌生,我在解决这个错误时遇到了麻烦,但只有当我注释掉 while 语句时才会发生这种情况,基本上我是从控制台询问值并将其存储在 HashMap 中:

use std::collections::HashMap;
use std::io;

fn main() {
    let mut customers = HashMap::new();
    let mut next_customer = true;
    while next_customer {
        let mut input_string = String::new();
        let mut temp_vec = Vec::with_capacity(3);
        let mut vec = Vec::with_capacity(2);
        println!("Insert new customer f.e = customer id,name,address:");
        io::stdin().read_line(&mut input_string);
        input_string = input_string.trim().to_string();
        for s in input_string.split(",") {
            temp_vec.push(s);
        }
        vec.push(temp_vec[1]);
        vec.push(temp_vec[2]);
        let mut key_value = temp_vec[0].parse::<i32>().unwrap();
        customers.insert(key_value, vec);
        next_customer = false;
    }
    println!("DONE");
}
Run Code Online (Sandbox Code Playgroud)

该代码导致错误

error[E0597]: `input_string` does …
Run Code Online (Sandbox Code Playgroud)

rust

4
推荐指数
1
解决办法
3817
查看次数

错误[E0308]:类型不匹配 - 预期为“&amp;str”,但找到了结构“std::string::String”

我正在制作井字游戏。代码不完整,但我被困在这里。我想打印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");
    } …
Run Code Online (Sandbox Code Playgroud)

type-mismatch rust

2
推荐指数
1
解决办法
9223
查看次数

标签 统计

rust ×2

type-mismatch ×1