“while let”

时间:2017-01-12 20:24:23

标签: rust borrow-checker

当我添加代码以改变items变量时,会出现问题。 以下代码示例无法编译:

#[derive(Debug)]
struct Item {
    value: u16,
    weight: u16,
    name: &'static str,
    //ratio: f32
}

impl PartialEq for Item {
    fn eq(&self, other: &Item) -> bool {
        self.name == other.name
    }
}

#[derive(Debug)]
struct KnapSack<'a> {
    capacity: u16,
    current: u16,
    items: Vec<&'a Item>
}

fn main() {
    let mut items = vec![
        Item { name: "map", weight: 9, value: 150},
        Item { name: "compass", weight: 13, value: 35},
        Item { name: "water",   weight: 153, value: 200},
        Item { name: "sandwich", weight: 50, value: 160},
        Item { name: "glucose", weight: 15, value: 60},
        Item { name: "tin", weight: 68, value: 45},
        Item { name: "banana", weight: 27, value: 60},
        Item { name: "apple", weight: 39, value: 40},
        Item { name: "cheese", weight: 23, value: 30},
        Item { name: "beer", weight: 52, value: 10},
        Item { name: "suntan cream", weight: 11, value: 70},
        Item { name: "camera", weight: 32, value: 30},
        Item { name: "T-shirt", weight: 24, value: 15},
        Item { name: "trousers", weight: 48, value: 10},
        Item { name: "umbrella", weight: 73, value: 40},
        Item { name: "waterproof trousers", weight: 42, value: 70},
        Item { name: "waterproof overclothes", weight: 43, value: 75},
        Item { name: "note-case", weight: 22, value: 80},
        Item { name: "sunglasses", weight: 7, value: 20},
        Item { name: "towel", weight: 18, value: 12},
        Item { name: "socks", weight: 4, value: 50},
        Item { name: "book", weight: 30, value: 10 },
    ];
    let mut knapsack = KnapSack{ capacity: 400, current: 0, items: vec![]};

    while let Some(item_ref) = take_highest_value_item(&items, knapsack.capacity - knapsack.current) {
        knapsack.current += (*item_ref).weight;
        knapsack.items.push(item_ref);

        items
            .iter()
            .position(|&n| n == *item_ref)
            .map(|e| items.remove(e));
    }

    println!("Items : {:?}", knapsack.items);
}



fn take_highest_value_item(items: &Vec<Item>, max_weight: u16) -> Option<&Item> {
    items
        .iter()
        .fold(None, |acc, item_ref| {
            if (*item_ref).weight > max_weight {
                return acc;
            }

            match acc {
                None =>
                    Some(item_ref),
                Some(acc_item) =>
                    if acc_item.value > (*item_ref).value {
                        acc
                    } else {
                        Some(item_ref)
                    }
            }  
        })
}

这是编译器消息:

error[E0502]: cannot borrow `items` as mutable because it is also borrowed as immutable


 --> src\main.rs:58:18
   |
51 |     while let Some(item_ref) = take_highest_value_item(&items, knapsack.capacity - knapsack.current) {
   |                                                         ----- immutable borrow occurs here
...
58 |             .map(|e| items.remove(e));
   |                  ^^^ ----- borrow occurs due to use of `items` in closure
   |                  |
   |                  mutable borrow occurs here
...
62 | }
   | - immutable borrow ends here

error[E0507]: cannot move out of borrowed content
  --> src\main.rs:57:24
   |
57 |             .position(|&n| n == *item_ref)
   |                        ^-
   |                        ||
   |                        |hint: to prevent move, use `ref n` or `ref mut n`
   |                        cannot move out of borrowed content

任何关于如何修复它的提示都会很棒!

0 个答案:

没有答案