convert forward into a method
This commit is contained in:
parent
d2f123bcf6
commit
1cc78f1e54
24
src/main.rs
24
src/main.rs
@ -20,10 +20,12 @@ mod rover {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn forward(slf : &Rover) -> Rover {
|
||||
let Rover(lon, lat, direction) = slf;
|
||||
let (xoff, yoff) = velocity(&direction);
|
||||
Rover(lon + xoff as i32, lat + yoff as i32, *direction)
|
||||
impl Rover {
|
||||
pub fn forward(&self) -> Rover {
|
||||
let Rover(lon, lat, direction) = self;
|
||||
let (xoff, yoff) = velocity(&direction);
|
||||
Rover(lon + xoff as i32, lat + yoff as i32, *direction)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn backward(slf : &Rover) -> Rover {
|
||||
@ -65,7 +67,7 @@ mod driver {
|
||||
|
||||
fn perform_command(command: &str, rover: &rover::Rover) -> rover::Rover {
|
||||
match command {
|
||||
"f" => rover::forward(&rover),
|
||||
"f" => rover.forward(),
|
||||
"b" => rover::backward(&rover),
|
||||
"l" => rover::rotate(&rover, rover::Rotation::Left),
|
||||
"r" => rover::rotate(&rover, rover::Rotation::Right),
|
||||
@ -91,39 +93,39 @@ mod tests {
|
||||
#[test]
|
||||
fn move_north() {
|
||||
let r = Rover(1 as i32, 1 as i32, rover::Facing::N);
|
||||
assert_eq!(rover::forward(&r),
|
||||
assert_eq!(r.forward(),
|
||||
Rover(1 as i32, 0 as i32, rover::Facing::N));
|
||||
|
||||
let r = Rover(1 as i32, 3 as i32, rover::Facing::N);
|
||||
assert_eq!(rover::forward(&r),
|
||||
assert_eq!(r.forward(),
|
||||
Rover(1 as i32, 2 as i32, rover::Facing::N));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn move_south() {
|
||||
let r = Rover(1 as i32, 1 as i32, rover::Facing::S);
|
||||
assert_eq!(rover::forward(&r),
|
||||
assert_eq!(r.forward(),
|
||||
Rover(1 as i32, 2 as i32, rover::Facing::S));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn move_west() {
|
||||
let r = Rover(1 as i32, 1 as i32, rover::Facing::W);
|
||||
assert_eq!(rover::forward(&r),
|
||||
assert_eq!(r.forward(),
|
||||
Rover(0 as i32, 1 as i32, rover::Facing::W));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn move_east() {
|
||||
let r = Rover(1 as i32, 1 as i32, rover::Facing::E);
|
||||
assert_eq!(rover::forward(&r),
|
||||
assert_eq!(r.forward(),
|
||||
Rover(2 as i32, 1 as i32, rover::Facing::E));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn move_backward() {
|
||||
let r = Rover(1 as i32, 1 as i32, rover::Facing::E);
|
||||
let r2 = rover::forward(&r);
|
||||
let r2 = r.forward();
|
||||
assert_eq!(rover::backward(&r2), r);
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user