extract velocity function

note we have to pass a reference to "facing" not the
tuple itself, otherwise it owns it and we get
"value used here after move"
This commit is contained in:
Daniel Barlow 2023-10-31 23:32:20 +00:00
parent 2b65145621
commit d8c0a21985

View File

@ -8,13 +8,17 @@ mod rover {
type Rover = (i32, i32, Facing); type Rover = (i32, i32, Facing);
pub fn forward((lon, lat, direction) : Rover) -> Rover { fn velocity(direction : &Facing) -> (i32, i32) {
let (xoff, yoff) = match direction { match direction {
Facing::N => ( 0, -1), Facing::N => ( 0, -1),
Facing::E => ( 1, 0), Facing::E => ( 1, 0),
Facing::S => ( 0, 1), Facing::S => ( 0, 1),
Facing::W => (-1, 0) Facing::W => (-1, 0)
}; }
}
pub fn forward((lon, lat, direction) : Rover) -> Rover {
let (xoff, yoff) = velocity(&direction);
(lon + xoff as i32, lat + yoff as i32, direction) (lon + xoff as i32, lat + yoff as i32, direction)
} }
} }