1 use std::fmt::{Debug, Formatter, Error};
2 use std::collections::HashMap;
3
4 trait HasInventory {
getInventory<'s>(&'s self) -> &'s mut Inventory5 fn getInventory<'s>(&'s self) -> &'s mut Inventory;
addToInventory(&self, item: &Item)6 fn addToInventory(&self, item: &Item);
removeFromInventory(&self, itemName: &str) -> bool7 fn removeFromInventory(&self, itemName: &str) -> bool;
8 }
9
10 trait TraversesWorld {
attemptTraverse(&self, room: &Room, directionStr: &str) -> Result<&Room, &str>11 fn attemptTraverse(&self, room: &Room, directionStr: &str) -> Result<&Room, &str> {
12 let direction = str_to_direction(directionStr);
13 let maybe_room = room.direction_to_room.get(&direction);
14 match maybe_room {
15 Some(entry) => Ok(entry),
16 //~^ ERROR lifetime mismatch [E0623]
17 _ => Err("Direction does not exist in room.")
18 }
19 }
20 }
21
22
23 #[derive(Debug, Eq, PartialEq, Hash)]
24 enum RoomDirection {
25 West,
26 East,
27 North,
28 South,
29 Up,
30 Down,
31 In,
32 Out,
33
34 None
35 }
36
37 struct Room {
38 description: String,
39 items: Vec<Item>,
40 direction_to_room: HashMap<RoomDirection, Room>,
41 }
42
43 impl Room {
new(description: &'static str) -> Room44 fn new(description: &'static str) -> Room {
45 Room {
46 description: description.to_string(),
47 items: Vec::new(),
48 direction_to_room: HashMap::new()
49 }
50 }
51
add_direction(&mut self, direction: RoomDirection, room: Room)52 fn add_direction(&mut self, direction: RoomDirection, room: Room) {
53 self.direction_to_room.insert(direction, room);
54 }
55 }
56
57 struct Item {
58 name: String,
59 }
60
61 struct Inventory {
62 items: Vec<Item>,
63 }
64
65 impl Inventory {
new() -> Inventory66 fn new() -> Inventory {
67 Inventory {
68 items: Vec::new()
69 }
70 }
71 }
72
73 struct Player {
74 name: String,
75 inventory: Inventory,
76 }
77
78 impl Player {
new(name: &'static str) -> Player79 fn new(name: &'static str) -> Player {
80 Player {
81 name: name.to_string(),
82 inventory: Inventory::new()
83 }
84 }
85 }
86
87 impl TraversesWorld for Player {
88 }
89
90 impl Debug for Player {
fmt(&self, formatter: &mut Formatter) -> Result<(), Error>91 fn fmt(&self, formatter: &mut Formatter) -> Result<(), Error> {
92 formatter.write_str("Player{ name:");
93 formatter.write_str(&self.name);
94 formatter.write_str(" }");
95 Ok(())
96 }
97 }
98
str_to_direction(to_parse: &str) -> RoomDirection99 fn str_to_direction(to_parse: &str) -> RoomDirection {
100 match to_parse {
101 "w" | "west" => RoomDirection::West,
102 "e" | "east" => RoomDirection::East,
103 "n" | "north" => RoomDirection::North,
104 "s" | "south" => RoomDirection::South,
105 "in" => RoomDirection::In,
106 "out" => RoomDirection::Out,
107 "up" => RoomDirection::Up,
108 "down" => RoomDirection::Down,
109 _ => None
110 }
111 //~^^ ERROR `match` arms have incompatible types
112 }
113
main()114 fn main() {
115 let mut player = Player::new("Test player");
116 let mut room = Room::new("A test room");
117 println!("Made a player: {:?}", player);
118 println!("Direction parse: {:?}", str_to_direction("east"));
119 match player.attemptTraverse(&room, "west") {
120 Ok(_) => println!("Was able to move west"),
121 Err(msg) => println!("Not able to move west: {}", msg)
122 };
123 }
124