From d8c0a21985b2b4c40de7d6fabb3fab1deb73f2ed Mon Sep 17 00:00:00 2001 From: Daniel Barlow Date: Tue, 31 Oct 2023 23:32:20 +0000 Subject: [PATCH] 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" --- src/main.rs | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/main.rs b/src/main.rs index 01e8c35..7d36a4d 100644 --- 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) } }