解构是Rust的高级特性.
pub fn all_or_nothing_system(
query: Query<(Entity, &AllOrNothing, &ScorerSpan)>,
mut scores: Query<&mut Score>,
) {
for (
aon_ent,
AllOrNothing {
threshold,
scorers: children, // 这里解构时为字段进行了重命名.
},
_span,
) in query.iter()
{
let mut sum = 0.0;
for Scorer(child) in children.iter() {
let score = scores.get_mut(*child).expect("where is it?");
if score.0 < *threshold {
sum = 0.0;
break;
} else {
sum += score.0;
}
}
let mut score = scores.get_mut(aon_ent).expect("where did it go?");
score.set(crate::evaluators::clamp(sum, 0.0, 1.0));
#[cfg(feature = "trace")]
{
let _guard = _span.span().enter();
trace!("AllOrNothing score: {}", score.get());
}
}
}
再看一个例子.
struct Point {
x: i32,
y: i32,
}
fn main() {
let p = Point { x: 10, y: 20 };
let Point { x: horizontal, y: vertical } = p;
println!("Horizontal: {}, Vertical: {}", horizontal, vertical);
}