我应该如何处理生锈的这一举动?
How should I handle this move in rust?
我对生命周期和借用的工作原理有基本的了解,但我在实践中才刚刚开始理解。
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
我想要一个枚举,表示已选择哪一餐,并将其分配给我用来表示餐点的结构实例。我不明白为什么会出现这个错误,因为在我看来这个所有者 'meal' 将在循环的每次迭代中重新分配。
你有 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,
}
在你的枚举之上。
我对生命周期和借用的工作原理有基本的了解,但我在实践中才刚刚开始理解。
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
我想要一个枚举,表示已选择哪一餐,并将其分配给我用来表示餐点的结构实例。我不明白为什么会出现这个错误,因为在我看来这个所有者 'meal' 将在循环的每次迭代中重新分配。
你有 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,
}
在你的枚举之上。