如何修复我的代码,以便 HashMap hmap_for_data 保存不可变向量 aux_vec_i32 中的项目计数?

问题描述 投票:0回答:1

这是我为 leetcode 问题编写的代码的简化版本。在 leetcode 的问题中,我有一个像下面的 aux_vec_i32 这样的不可变输入,我需要返回比下面的示例代码更复杂的东西,但是我编写的 leetcode 内容和下面的内容之间的编译器错误是相似的。最后 hmap_for_data 应该保存 aux_vec_i32 中项目的唯一计数。

当前代码中散布的“&”、“mut”、“*”和“as”咒语是我与编译器对抗的结果,并且没有按照编译器的建议取得成功。

use std::collections::HashMap;

fn main(){
    struct Data{
        the_data: i32,
    }
    let aux_vec_i32 = vec![1, 2, 3, 1];
    let mut hmap_for_data: HashMap<i32, Data>=HashMap::new();

    for i in &aux_vec_i32 {
        match hmap_for_data.get(&mut aux_vec_i32[*i as usize]) {
            None => {
                hmap_for_data.insert(
                    aux_vec_i32[*i as usize], 
                    Data {
                        the_data: 1,
                    }
                );
            }
            Some(mut data_to_increment) => {
                data_to_increment.the_data += 1;
            }
        }
    }
}
error[E0502]: cannot borrow `aux_vec_i32` as mutable because it is also borrowed as immutable
  --> src/main.rs:11:38
   |
10 |     for i in &aux_vec_i32 {
   |              ------------
   |              |
   |              immutable borrow occurs here
   |              immutable borrow later used here
11 |         match hmap_for_data.get(&mut aux_vec_i32[*i as usize]) {
   |                                      ^^^^^^^^^^^ mutable borrow occurs here

error[E0596]: cannot borrow `aux_vec_i32` as mutable, as it is not declared as mutable
  --> src/main.rs:11:38
   |
11 |         match hmap_for_data.get(&mut aux_vec_i32[*i as usize]) {
   |                                      ^^^^^^^^^^^ cannot borrow as mutable
   |
help: consider changing this to be mutable
   |
7  |     let mut aux_vec_i32 = vec![1, 2, 3, 1];
   |         +++

error[E0594]: cannot assign to `data_to_increment.the_data`, which is behind a `&` reference
  --> src/main.rs:21:17
   |
20 |             Some(mut data_to_increment) => {
   |                  --------------------- consider changing this binding's type to be: `&mut Data`
21 |                 data_to_increment.the_data += 1;
   |                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `data_to_increment` is a `&` reference, so the data it refers to cannot be written

这里是游乐场的链接:https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=535206613151ae0e4507296cf816d2a0.

rust borrow-checker
1个回答
0
投票

这里是修改后的代码,为了使其能够通过编译。

说实话,我不明白它的作用,因为向量中的索引和内容之间存在某种混合......

use std::{collections::HashMap, fmt::Debug};

fn main() {
    #[derive(Debug)]
    struct Data {
        the_data: i32,
    }
    let aux_vec_i32 = vec![1, 2, 3, 1];
    let mut hmap_for_data: HashMap<i32, Data> = HashMap::new();

    for i in &aux_vec_i32 {
        match hmap_for_data.get_mut(&aux_vec_i32[*i as usize]) {
            None => {
                hmap_for_data
                    .insert(aux_vec_i32[*i as usize], Data { the_data: 1 });
            }
            Some(data_to_increment) => {
                data_to_increment.the_data += 1;
            }
        }
    }
    println!("{:?}", hmap_for_data);
}
/*
{2: Data { the_data: 2 }, 3: Data { the_data: 1 }, 1: Data { the_data: 1 }}
*/
© www.soinside.com 2019 - 2024. All rights reserved.