alice_bob/
alice_bob.rs

1use riddance::{Id, Registry};
2
3struct Person {
4    name: String,
5    friends: Vec<PersonId>,
6}
7
8type People = Registry<Person>;
9type PersonId = Id<Person>;
10
11fn new_person(people: &mut People, name: &str) -> PersonId {
12    people.insert(Person {
13        name: name.into(),
14        friends: Vec::new(),
15    })
16}
17
18fn add_friend(people: &mut People, this_id: PersonId, other_id: PersonId) {
19    if people[other_id].name != people[this_id].name {
20        people[this_id].friends.push(other_id);
21    }
22}
23
24fn main() {
25    let mut people = People::new();
26    let alice_id = new_person(&mut people, "Alice");
27    let bob_id = new_person(&mut people, "Bob");
28    add_friend(&mut people, alice_id, bob_id);
29    add_friend(&mut people, bob_id, alice_id);
30    add_friend(&mut people, alice_id, alice_id); // no-op
31}