0

I haven't been able to figure this one out, feeling a bit lost.

The goal of this program is to generate random numbers from 1-50, and to stop looping once it hits the user's input.

Currently I've added 5 as a placeholder, and I'm having trouble transferring the ownership of ui, which is user input, to random_gen().

I tried too many permutations to remember, but providing arguments their types in the function parameters was what I tried. At best I could get it to compile but it would change the 'ui' in the string of the match statement to the random number

use rand::Rng;
use std::io;

fn main() {
    //generate random number for random_num between 1 and 50
    println!("Please input the number you would like to stop on.");
    let mut ui = String::new();

    io::stdin().read_line(&mut ui).expect("Failed to read line");
    let ui: u32 = match ui.trim().parse() {
        Ok(num) => num,
        Err(_) => return,
    };

    println!("{} is your input", ui);
    //make copy variable inside function
    random_gen(ui);
}

fn random_gen(ui: u32) {
    loop {
        let random_num: u32 = rand::thread_rng().gen_range(1..=50);
        println!("{}", random_num);

        match random_num {
            //replace 5 with ui
            5 => {
                println!("With a bit of brain power, we got to {}", 5);
                break;
            }
            _ => {
                continue;
            }
        }
    }
}
Jmb
  • 18,893
  • 2
  • 28
  • 55

0 Answers0