繁体   English   中英

Rust - BTreeMap 未正确更新向量值

[英]Rust - BTreeMap not updating vector in value correctly

我在使用 BTreeMaps 时遇到了一个问题; 程序运行时它们似乎没有更新。 这里有一个例子:

fn monke_do_monke_bisnis(mut monkes: BTreeMap<i32, Monke>) -> BTreeMap<i32, Monke> {
    for mut monke in monkes.to_owned() {
        println!("Monkey: {:?}", monke.0);
        let mut new_prio: f32 = 0.0;
        for i in monke.1.inventory.to_owned() {
            println!("Inventory length: {:?}", monke.1.inventory.len());
            println!("Inspected item: {:?}", i);

            let last = parse_last(i, monke.1.operation.last.clone());
            match monke.1.operation.operand {
                '*' => {
                    new_prio = ((i * last)) as f32
                }
                '/' => {
                    new_prio = ((i / last)) as f32
                }
                '+' => {
                    new_prio = ((i + last)) as f32
                }
                '-' => {
                    new_prio = ((i - last)) as f32
                }
                _ => panic!("need op bruv"),
            }
            println!("New worry level of item: {:?}", new_prio);
            
            new_prio /= 3.0;
            new_prio = new_prio.floor();
            println!("New worry level of item / 3: {:?}", new_prio);
            

            if (new_prio as i32) % monke.1.test == 0 {
                monkes.entry(monke.1.true_outcome).and_modify(|monk| {
                    monk.inventory.push(new_prio as i32);
                    println!("TRUE: Thrown to: {:?}", monk.id);                    
                    println!("Inventory of {:?}: {:?}", monk.id, monk.inventory);
                });
            } else {
                monkes.entry(monke.1.false_outcome).and_modify(|monk| {
                    monk.inventory.push(new_prio as i32);     
                    println!("FALSE: Thrown to: {:?}", monk.id);
                    println!("Inventory of {:?}: {:?}", monk.id, monk.inventory);

                });
            }
            //remove item from original monke
            monkes
                .entry(monke.1.id)
                .and_modify(|monk| {
                    monk.inventory.remove(0);
                    monk.inspect_count += 1;
                });
        }
    }
    return monkes;
}

以下是结构:


#[derive(Default, Debug, Clone)]
struct Monke {
    id: i32,
    inventory: Vec<i32>,
    operation: Operation,
    test: i32,
    true_outcome: i32,
    false_outcome: i32,
    inspect_count: i32,
}

#[derive(Default, Debug, Clone)]
struct Operation {
    operand: char,
    last: String,
}

这是让它运行时的控制台 output:

Monke { id: 0, inventory: [20, 23, 27, 26], operation: Operation { operand: '*', last: "19" }, test: 23, true_outcome: 2, false_outcome: 3, inspect_count: 2 }
Monke { id: 1, inventory: [25], operation: Operation { operand: '+', last: "6" }, test: 19, true_outcome: 2, false_outcome: 0, inspect_count: 4 }
Monke { id: 2, inventory: [], operation: Operation { operand: '*', last: "old" }, test: 13, true_outcome: 1, false_outcome: 3, inspect_count: 3 }
Monke { id: 3, inventory: [500, 620, 1200, 3136], operation: Operation { operand: '+', last: "3" }, test: 17, true_outcome: 0, false_outcome: 1, inspect_count: 1 }

这里应该是 output:

Monke { id: 0, inventory: [20, 23, 27, 26], operation: Operation { operand: '*', last: "19" }, test: 23, true_outcome: 2, false_outcome: 3, inspect_count: 2 }
Monke { id: 1, inventory: [2080, 25, 167, 207, 401, 1046], operation: Operation { operand: '+', last: "6" }, test: 19, true_outcome: 2, false_outcome: 0, inspect_count: 4 }
Monke { id: 2, inventory: [], operation: Operation { operand: '*', last: "old" }, test: 13, true_outcome: 1, false_outcome: 3, inspect_count: 3 }
Monke { id: 3, inventory: [], operation: Operation { operand: '+', last: "3" }, test: 17, true_outcome: 0, false_outcome: 1, inspect_count: 1 }

似乎我正在使用的 BTreeMap 没有正确更新,因为逻辑是正确的。 可能是什么原因造成的?

您正在monkes的开头克隆僧侣(通过to_owned() ),并迭代该 object 而不是原始的。 在循环的未来迭代中,当您需要 Monkey 1 时,您仍然从克隆的 map 中读取它的原始值。您使用的条目模式可能很难工作,因为您需要修改集合中的多个项目同时,但我不知道 go 和您拥有的 map 的最佳解决方案(我会使用Vec ,因为索引可以对应于 ID)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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