1 // Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 use std
::fmt
::{Debug, Formatter, Error}
;
12 use std
::collections
::HashMap
;
15 fn getInventory
<'s
>(&'s
self) -> &'s
mut Inventory
;
16 fn addToInventory(&self, item
: &Item
);
17 fn removeFromInventory(&self, itemName
: &str) -> bool
;
20 trait TraversesWorld
{
21 fn attemptTraverse(&self, room
: &Room
, directionStr
: &str) -> Result
<&Room
, &str> {
22 let direction
= str_to_direction(directionStr
);
23 let maybe_room
= room
.direction_to_room
.get(&direction
);
25 Some(entry
) => Ok(entry
),
26 //~^ ERROR 25:28: 25:37: lifetime mismatch [E0623]
27 _
=> Err("Direction does not exist in room.")
33 #[derive(Debug, Eq, PartialEq, Hash)]
50 direction_to_room
: HashMap
<RoomDirection
, Room
>,
54 fn new(description
: &'
static str) -> Room
{
56 description
: description
.to_string(),
58 direction_to_room
: HashMap
::new()
62 fn add_direction(&mut self, direction
: RoomDirection
, room
: Room
) {
63 self.direction_to_room
.insert(direction
, room
);
76 fn new() -> Inventory
{
89 fn new(name
: &'
static str) -> Player
{
91 name
: name
.to_string(),
92 inventory
: Inventory
::new()
97 impl TraversesWorld
for Player
{
100 impl Debug
for Player
{
101 fn fmt(&self, formatter
: &mut Formatter
) -> Result
<(), Error
> {
102 formatter
.write_str("Player{ name:");
103 formatter
.write_str(&self.name
);
104 formatter
.write_str(" }");
109 fn str_to_direction(to_parse
: &str) -> RoomDirection
{
110 match to_parse
{ //~ ERROR match arms have incompatible types
111 "w" | "west" => RoomDirection
::West
,
112 "e" | "east" => RoomDirection
::East
,
113 "n" | "north" => RoomDirection
::North
,
114 "s" | "south" => RoomDirection
::South
,
115 "in" => RoomDirection
::In
,
116 "out" => RoomDirection
::Out
,
117 "up" => RoomDirection
::Up
,
118 "down" => RoomDirection
::Down
,
124 let mut player
= Player
::new("Test player");
125 let mut room
= Room
::new("A test room");
126 println
!("Made a player: {:?}", player
);
127 println
!("Direction parse: {:?}", str_to_direction("east"));
128 match player
.attemptTraverse(&room
, "west") {
129 Ok(_
) => println
!("Was able to move west"),
130 Err(msg
) => println
!("Not able to move west: {}", msg
)