Skip to content

Commit be89206

Browse files
committed
Update function to borrow self, not take ownership
As @petertseng pointed out, I can update these functions to borrow `self` if I give Direction and Position `Copy` exercism#146 (comment)
1 parent bcc23ab commit be89206

File tree

1 file changed

+7
-6
lines changed

1 file changed

+7
-6
lines changed

exercises/robot-simulator/example.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#[derive(PartialEq, Debug, Clone)]
1+
#[derive(PartialEq, Debug, Copy, Clone)]
22
pub enum Direction {
33
North,
44
East,
@@ -26,6 +26,7 @@ impl Direction {
2626
}
2727
}
2828

29+
#[derive(Clone, Copy)]
2930
struct Position {
3031
x: i32,
3132
y: i32,
@@ -36,7 +37,7 @@ impl Position {
3637
Position { x: x, y: y }
3738
}
3839

39-
fn advance(self, direction: &Direction) -> Self {
40+
fn advance(&self, direction: &Direction) -> Self {
4041
match direction {
4142
&Direction::North => Self::new(self.x, self.y + 1),
4243
&Direction::South => Self::new(self.x, self.y - 1),
@@ -63,19 +64,19 @@ impl Robot {
6364
}
6465
}
6566

66-
pub fn turn_right(self) -> Self {
67+
pub fn turn_right(&self) -> Self {
6768
Self::build(self.position, self.direction.next_clockwise())
6869
}
6970

70-
pub fn turn_left(self) -> Self {
71+
pub fn turn_left(&self) -> Self {
7172
Self::build(self.position, self.direction.previous_clockwise())
7273
}
7374

74-
pub fn advance(self) -> Self {
75+
pub fn advance(&self) -> Self {
7576
Self::build(self.position.advance(&self.direction), self.direction)
7677
}
7778

78-
pub fn instructions(self, instructions: &str) -> Self {
79+
pub fn instructions(&self, instructions: &str) -> Self {
7980
let mut r = Self::build(self.position, self.direction);
8081
for c in instructions.chars() {
8182
r = r.execute(c);

0 commit comments

Comments
 (0)