commit d8c0a21985b2b4c40de7d6fabb3fab1deb73f2ed
parent 2b65145621ffbacaf13187df4dfb443996bab20c
Author: Daniel Barlow <dan@telent.net>
Date: Tue, 31 Oct 2023 23:32:20 +0000
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"
Diffstat:
1 file changed, 7 insertions(+), 3 deletions(-)
diff --git a/src/main.rs b/src/main.rs
@@ -8,13 +8,17 @@ mod rover {
type Rover = (i32, i32, Facing);
- pub fn forward((lon, lat, direction) : Rover) -> Rover {
- let (xoff, yoff) = match direction {
+ fn velocity(direction : &Facing) -> (i32, i32) {
+ match direction {
Facing::N => ( 0, -1),
Facing::E => ( 1, 0),
Facing::S => ( 0, 1),
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)
}
}