1 use std::fmt::{Debug, Formatter, Error};
2 use std::collections::HashMap;
5 fn getInventory<'s>(&'s self) -> &'s mut Inventory;
6 fn addToInventory(&self, item: &Item);
7 fn removeFromInventory(&self, itemName: &str) -> bool;
10 trait TraversesWorld {
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);
15 Some(entry) => Ok(entry),
16 //~^ ERROR lifetime mismatch [E0623]
17 _ => Err("Direction does not exist in room.")
23 #[derive(Debug, Eq, PartialEq, Hash)]
40 direction_to_room: HashMap<RoomDirection, Room>,
44 fn new(description: &'static str) -> Room {
46 description: description.to_string(),
48 direction_to_room: HashMap::new()
52 fn add_direction(&mut self, direction: RoomDirection, room: Room) {
53 self.direction_to_room.insert(direction, room);
66 fn new() -> Inventory {
79 fn new(name: &'static str) -> Player {
81 name: name.to_string(),
82 inventory: Inventory::new()
87 impl TraversesWorld for Player {
90 impl Debug for Player {
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(" }");
99 fn str_to_direction(to_parse: &str) -> RoomDirection {
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,
111 //~^^ ERROR `match` arms have incompatible types
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)