【发布时间】: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