1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
use crate::z_ignore_test_common::*;
use flecs_ecs::prelude::*;
#[derive(Component)]
pub struct Eats;
#[derive(Component)]
struct Healthy;
fn main() {
let world = World::new();
let apples = world.entity_named("Apples").add(Healthy);
let salad = world.entity_named("Salad").add(Healthy);
let burgers = world.entity_named("Burgers");
let pizza = world.entity_named("Pizza");
let chocolate = world.entity_named("Chocolate");
world
.entity_named("Bob")
.add((Eats, apples))
.add((Eats, burgers))
.add((Eats, pizza));
world
.entity_named("Alice")
.add((Eats, salad))
.add((Eats, chocolate))
.add((Eats, apples));
// Here we're creating a rule that in the query DSL would look like this:
// Eats($This, $Food), Healthy($Food)
//
// example shows how the basics of how to use queries & variables.
let rule = world
.query::<()>()
// Identifiers that start with _ are query variables. Query variables
// are like wildcards, but enforce that the entity substituted by the
// wildcard is the same across terms.
//
// For example, in this query it is not guaranteed that the entity
// substituted by the * for Eats is the same as for Healthy:
// (Eats, *), Healthy(*)
//
// By replacing * with _Food, both terms are constrained to use the
// same entity.
.with((Eats, "$food"))
.with(&Healthy)
.set_src("$food")
.build();
// Lookup the index of the variable. This will let us quickly lookup its
// value while we're iterating.
let food_var = rule.find_var("food");
// Iterate the rule
rule.each_iter(|it, index, ()| {
println!(
"{} eats {}",
it.entity(index).name(),
it.get_var(food_var.unwrap()).name()
);
});
// Output:
// Bob eats Apples
// Alice eats Apples
// Alice eats Salad
}
#[cfg(feature = "flecs_nightly_tests")]
#[test]
fn test() {
let output_capture = OutputCapture::capture().unwrap();
main();
output_capture.test("query_variables".to_string());
}