Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // https://play.rust-lang.org/?gist=00a5ed94a488279e73167c48c444761c
- // Fix
- extern crate rand;
- use rand::prelude::{Rng, ThreadRng};
- #[derive(Clone, Copy, Eq, PartialEq, Debug)]
- pub enum Move {
- Right, Up, Left, Down, Stop,
- }
- fn main() {
- let m1 = random_move_if::<ThreadRng>(None);
- let mut rng: Option<ThreadRng> = Some(rand::thread_rng());
- let m2 = random_move_if(rng.as_mut());
- println!("m1, m2 = {:?}, {:?}", m1, m2);
- }
- fn random_move_if<R: Rng>(random: Option<&mut R>) -> Move {
- let moves = vec![Move::Right, Move::Up, Move::Left, Move::Down];
- // match random {
- // None => Move::Stop,
- // Some(r) => moves[r.gen_range(0, moves.len())],
- // }
- if random.is_some() {
- let r = random.unwrap();
- moves[r.gen_range(0, 4)]
- } else {
- Move::Stop
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement