【问题标题】:How should I handle this move in rust?我应该如何处理这个生锈的举动?
【发布时间】:2021-07-11 06:09:38
【问题描述】:

我对生命周期和借贷的运作方式有基本的了解,但在实践中我才刚刚开始了解。

enum MealTime {
    Breakfast,
    Lunch,
    Dinner,
}
struct Item {
    name: String,
    station: String,
    meal: MealTime,
}
    for meal in [MealTime::Breakfast, MealTime::Lunch, MealTime::Dinner] {

        let main_section = doc
            .select(match &meal {
                MealTime::Breakfast => &bsectionsel,
                MealTime::Lunch => &lsectionsel,
                MealTime::Dinner => &dsectionsel,
            })
            .next()
            .unwrap();

        for sections in main_section.select(&itemsectionsel) {
            let stationc = sections
                .select(&stationsel)
                .next()
                .unwrap()
                .text()
                .next()
                .unwrap()
                .trim();

            for itemname in sections.select(&buttonsel) {
                let itemnamec = itemname.text().next().unwrap().trim();

                let newitem = Item {
                    name: itemnamec.to_string(),
                    station: stationc.to_string(),
                    meal: meal,
                };

                item_list.push(newitem);

                println!("{}\nStation: {}\n", newitem.name, newitem.station,);
            }
        }
    }

给:error[E0382]: use of moved value: meal

meal: meal,
      ^^^^ value moved here, in previous iteration of loop

我想要一个枚举,代表已选择的餐点分配给我为表示餐点项目而制作的结构实例。我不明白为什么会发生这个错误,因为在我看来,这个所有者“餐”将在循环的每次迭代中重新分配。

【问题讨论】:

  • 但是你有 3 个嵌套循环,它在最外面的循环中重新分配,它不在内部循环中。您要么需要在用餐时调用派生并调用克隆(),或者如果您的枚举像这里一样简单,只需派生副本,每次移动时它都会复制。
  • @Prime_Aqasix 谢谢,不知道那个宏。

标签: rust borrow-checker


【解决方案1】:

您有 3 个嵌套循环,它在最外层循环中重新分配,而不是在内层循环中。

for meal in [MealTime::Breakfast, MealTime::Lunch, MealTime::Dinner] {

    let main_section = doc
        .select(match &meal {
            MealTime::Breakfast => &bsectionsel,
            MealTime::Lunch => &lsectionsel,
            MealTime::Dinner => &dsectionsel,
        })
        .next()
        .unwrap();

    for sections in main_section.select(&itemsectionsel) { //<-- second inner loop here

        let stationc = sections
            .select(&stationsel)
            .next()
            .unwrap()
            .text()
            .next()
            .unwrap()
            .trim();

        for itemname in sections.select(&buttonsel) {// <-- third inner loop here
            let itemnamec = itemname.text().next().unwrap().trim();

            let newitem = Item {
                name: itemnamec.to_string(),
                station: stationc.to_string(),
                meal: meal, // This is called multiple times per outermost loop
            };

            item_list.push(newitem);

            println!("{}\nStation: {}\n", newitem.name, newitem.station,);
        }
    }
}

如果你的枚举和你提供的代码一样简单,那就放

#[derive(Copy, Clone)]
enum MealTime {
    Breakfast,
    Lunch,
    Dinner,
}

在你的枚举之上。

Copy trait

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-01
    • 1970-01-01
    • 2011-09-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多