How to prevent a value from being moved?

前端 未结 2 1836
时光说笑
时光说笑 2020-12-04 03:06

I\'m having a lot of fun trying to solve the robot simulator Exercism exercise, but I\'m facing a value moving problem for which I don\'t seem to be able to come up with an

2条回答
  •  死守一世寂寞
    2020-12-04 03:35

    Looking at the other users answers, you can actually do it with fold :

    pub fn instructions(self, instructions: &str) -> Self {
        instructions.chars().fold(self, |robot, c| {
            match c {
                'L' => robot.turn_left(),
                'R' => robot.turn_right(),
                'A' => robot.advance(),
                _ => panic!("unexpected char")
            }
        })
    }
    

    It seems to keep moving back robot into the scope.

提交回复
热议问题