#Is it possible to use a variable as a match expression?

1 messages · Page 1 of 1 (latest)

lilac ridge
#

Not sure if that's the correct wording, but basically I have:

let current_recv_buff = match current_down_speed {
                0..=1000 => 4,
                1001..=10000 => 32,
                10001..=100000 => 512,
                100001..=1000000 => 4096,
                1000001.. => 16384,
                _ => 16384,
            };

But I repeat this table elsewhere in my code. Can I define it as a variable? Or will I have to make a function for it

pale solar
#

you should make a function with that match

leaden wren
#

seems like the perfect use case for just making it a function

#

you probably could use like a const array of tuples for how to convert the numbers, but doing that would just make it much less readable and wouldnt even make it less verbose. Just move the code into a reusable container: a function