dimanche 10 juillet 2022

can not convert const to number

I have a code which should give number 1 or 5 or 10 or 50. rust compiler says "pattern 4_u8..=u8::MAX not covered"

the code:

use rand::Rng;

fn main() {
    let rand_num: u8 = rand::thread_rng().gen_range(0, 4);
    println!("{}", rand_num);
    let coin: Coin;
    match rand_num{
        0 => coin = Coin::Penny,
        1 => coin = Coin::Nickel,
        2 => coin = Coin::Dime,
        3 => coin = Coin::Quarter,
    }
    println!("{}", value_in_cents(coin));
}

enum Coin {
    Penny,
    Nickel,
    Dime,
    Quarter,
}

fn value_in_cents(coin: Coin) -> u8 {
    match coin{
        Coin::Penny => return 1,
        Coin::Nickel => return 5,
        Coin::Dime => return 10,
        Coin::Quarter => return 25,
    }
}

i have no idea how can i fix it




Aucun commentaire:

Enregistrer un commentaire